tamtom
tamtom

Reputation: 2594

How to fill the null array with a specific number in Java ?

I'm trying to solve a palindrome problem that the input consists of Strings , if the concatenation of two strings represent a palindrome word(A palindrome is a word which can be read the same way in either direction. For example, the following words are palindromes: civic, radar, rotor, and madam) then save it into array to print it latter otherwise print "0" but I'm having a problem in filling the null index with zeros , here I get Exception

 for (int re = 0; re < result.length; re++) {
                if (result[re].equals(null)) {
                    result[re] = "0";
                }
            }
    "Exception in thread "main" java.lang.NullPointerException"

here is my full code

import java.util.Scanner;

public class Palindrome {

    public static String reverse(String R2) {
        String Reverse = "";
        String word_two = R2;

        int ln = word_two.length();
        for (int i = ln - 1; i >= 0; i--) {
            Reverse = Reverse + word_two.charAt(i);
        }
        return Reverse;
    }

    public static void main(String[] args) {
        Scanner inpoot = new Scanner(System.in);


        int stop = 0;
        String pal1;
        int Case = inpoot.nextInt();
        String result[] = new String[Case];
        String Final;
        int NumberofWords;
        for (int i = 0; i < Case; i++) {
            NumberofWords = inpoot.nextInt();

            String words[] = new String[NumberofWords];
            for (int array = 0; array < words.length; array++) {
                words[array] = inpoot.next();
            }

            for (int word1 = 0; word1 < NumberofWords; word1++) {
                if (stop > Case) {
                    break;
                }
                for (int word2 = 0; word2 < NumberofWords; word2++) {
                    if (word1 == word2) {
                        continue;
                    }

                    Final = "" + words[word1].charAt(0);
                    if (words[word2].endsWith(Final)) {
                        pal1 = words[word1].concat(words[word2]);
                    } else {
                        continue;
                    }
                    if (pal1.equals(reverse(pal1))) {

                        result[i] = pal1;
                        stop++;
                        break;
                    } else {
                        pal1 = "";
                    }

                }

            }


        }
        // HERE IS THE PROBLEM 
        for (int re = 0; re < result.length; re++) {
            if (result[re].equals(null)) {
                result[re] = "0";
            }
        }
        for (int x = 0; x < result.length; x++) {
            System.out.println("" + result[x]);
        }

    }

}

Upvotes: 0

Views: 627

Answers (2)

Seelenvirtuose
Seelenvirtuose

Reputation: 20618

If you want to check whether result[re] is null, you cannot use equals. Use the identity comparison:

if (result[re] == null) {
    result[re] = "0";
}

Upvotes: 0

Dici
Dici

Reputation: 25950

A test such as anObject.equals(null) makes no sense. Indeed, if anObject is null, it will throw a NullPointerException (NPE), and if it is not, it will always return false.

To test if a reference is null, just use anObject == null.

Upvotes: 1

Related Questions