Bishant Bhattarai
Bishant Bhattarai

Reputation: 85

My do while loop in java only runs once even when condition given satisfies it

My loop only runs once.

This code is suppose to take home team name, away team name, home team score, away team score and store it. It should only exit the loop when entered "EXIT". But the loop only runs once. The exit part works though. I am new to java so sorry if its just a small mistake.

I haven't handled any exceptions yet so the code is incomplete.

    String[] ht_name = new String[9999];
    String[] at_name = new String[9999];
    int[] ht_score = new int[9999];
    int[] at_score = new int[9999];
    Scanner scanint = new Scanner(System.in);
    Scanner scanstr = new Scanner(System.in);

    int i=0;
            //do while loop starts
    do {

        System.out.println("Enter Home team name: ");

        ht_name[i] = scanstr.nextLine();

            if(!ht_name[i].equalsIgnoreCase("exit"))
            {
                System.out.println("Enter Away team name: ");
                at_name[i] = scanstr.nextLine();

                System.out.println("Enter Home team score: ");
                ht_score[i] = scanint.nextInt();


                System.out.println("Enter Away team score: ");
                at_score[i] = scanint.nextInt();

                i++;
            }

        } while (!ht_name[i].equalsIgnoreCase("exit"));
       //do while loop ends
    }}

Upvotes: 0

Views: 151

Answers (3)

Pavel Smirnov
Pavel Smirnov

Reputation: 4799

This code gives you NullPointerException, because you increment i variable and right after that take a value at i index from ht_name array, which equals null.

Your code should probably look like this:

        String[] ht_name = new String[9999];
        String[] at_name = new String[9999];
        int[] ht_score = new int[9999];
        int[] at_score = new int[9999];
        Scanner scanint = new Scanner(System.in);
        Scanner scanstr = new Scanner(System.in);

        int i=0;
                //do while loop starts
        do {

            System.out.println("Enter Home team name: ");

            ht_name[i] = scanstr.nextLine();

                if(!ht_name[i].equalsIgnoreCase("exit"))
                {
                    System.out.println("Enter Away team name: ");
                    at_name[i] = scanstr.nextLine();

                    System.out.println("Enter Home team score: ");
                    ht_score[i] = scanint.nextInt();


                    System.out.println("Enter Away team score: ");
                    at_score[i] = scanint.nextInt();

                    i++;
                } else {
                    break; //this statement exits the loop each time when 'exit' is typed in 
                }

            } while (true); //while (true) gives you infinite loop. So this loop is broken only when 'exit' is typed in
           //do while loop ends

Upvotes: 2

Taslim Oseni
Taslim Oseni

Reputation: 6273

The problem is with your while logic. You are checking the value for an index that does not quite exist yet. Remember you already incremented. Think about it this way:

  • User inputs a string
  • You check whether the string is equal to "exit"
  • If it does, you perform an operation and then increment your counter
  • In your while loop, you then check whether the element in the previous index was "exit". If it was, you stop the loop.

As you can see, your mistake was in the final step. You were supposed to check the element in the previous index and not the one in the current index. Simply change the while condition to this:

while (!ht_name[i-1].equalsIgnoreCase("exit"))

I hope this helps.. Merry coding.

Upvotes: 1

Jabin
Jabin

Reputation: 31

Your index calculation error, The last line of code should look like this:

while (!ht_name[i-1].equalsIgnoreCase("exit"))

Upvotes: 3

Related Questions