Question

This is a program that stores 10 unique strings. If the user enters a string that already exists in the array, the user will get an error. My code is working perfectly for the first string that I enter, but throws an exception after that and I don't know why. How do I fix this and make it work?

P.S. I don't want to use a Set. I want to do it with an array.

Edit: Error name: Exception in thread "main" java.lang.NullPointerException Java Result: 1

Thanks.

 public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int stringNumber = 0;
        String[] stringArray = new String[10];

        for (int i = 0; i <= stringArray.length; i++) {

            boolean itemExists = false;

            out.println("\nEnter a string");
            String input = keyboard.next();

            if (i > 0) {
                for (int j = 0; j < stringArray.length; j++) {
                    if (stringArray[j].equalsIgnoreCase(input)) {
                        itemExists = true;
                        out.println("Item \"" + input + "\" already exists.");
                        break;
                    } else {
                        continue; // Unhandled exception error. If I don't have "continue" here, the program doesn't work properly after the first number.
                    }
                }
            }

            if (itemExists == false) {
                stringArray[stringNumber] = input;
                out.println("\"" + stringArray[stringNumber] + "\"" + " has been stored.");
            } else {
                out.println("Try again.");
            }

            PrintArray(stringArray);
            stringNumber++;

        }

    }

    public static void PrintArray(String[] stringArray) {

        for (int i = 0; i <= 9; i++) {
            if (stringArray[i] == null) {
                out.print(" ");
            } else {
                out.print("\nYour strings:");
                out.print(" " +stringArray[i] + " ");
            }
        }
    }
Was it helpful?

Solution

you problem is this if (stringArray[j].equalsIgnoreCase(input)) {, you refern to null element as you array do not contain nothing.

You can solve this

if (input.equalsIgnoreCase(stringArray[j])) {

Now you compare the value agains the array items that may be null.

Check the example code

    private final static Scanner keyboard = new Scanner(System.in);
    private final static String[] stringArray = new String[10];

    public static void main(String[] args) {

        int stringNumber = 0;
        for (int i = 0; i < stringArray.length; i++) {

            System.out.println("\nEnter a string");

            String input = readNextItem();

            if(isInputExist(input)) {
                System.out.println("Item \"" + input + "\" already exists.");
                System.out.println("Try again.");
            } else {
                stringArray[stringNumber++] = input;
                System.out.println("\"" + input + "\"" + " has been stored.");
            }

            printArray(stringArray);
        }

    }

    private static String readNextItem() {
        return keyboard.next();
    }

    private static boolean isInputExist(String input) {

        for(String stored : stringArray) {
            if(input.equalsIgnoreCase(stored)) {
                return true;
            }
        }
        return false;
    }

OTHER TIPS

You're getting the NullPointerException because you're accessing an empty array. In the second loop you're looping over the entire stringArray array but you've inserted the first i elements.

Change this:

for (int j = 0; j < stringArray.length; j++) {

with this:

for (int j = 0; j < i; j++) {

P.s. I suggest you to use a Set instead of the array.

Here a version with Set:

    int numStrings = 10;
    Set<String> inserted = new HashSet<String>();

    while(inserted.size()<=numStrings) {
        out.println("\nEnter a string");
        String input = keyboard.next();
        if (inserted.contains(input)) {
          out.println("Item \"" + input + "\" already exists.");
          out.println("Try again.");
        } else {
          inserted.add(input);
          out.println("\"" + input + "\"" + " has been stored.");
        }
    }

    out.println(inserted);

The problem is this inner loop:

           for (int j = 0; j < stringArray.length; j++) {
                if (stringArray[j].equalsIgnoreCase(input)) {
                    itemExists = true;
                    out.println("Item \"" + input + "\" already exists.");
                    break;
                } else {
                    continue; // Unhandled exception error. If I don't have "continue" here, the program doesn't work properly after the first number.
                }
            }

Its not the continue statement that causes the problem, its that you invoke

stringArray[j].equalsIgnoreCase(input)

in the if - stringArray[j] isn't yet filled with a String, it will cause the NullPointerException. You can just reverse the expression:

input.equalsIgnoreCase(stringArray[j])

(That is assuming "input" is never NULL)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top