0

The program is supposed to end and give the amount of correct answers once the last question is answered. Instead the program goes back to the initial question in the loop. "What is the capital of Alabama"

package exercise09_17;
import java.util.Scanner;

public class exercise09_17 {

    static Scanner input = new Scanner(System.in).useDelimiter("\r\n"); 
    public static void main(String[] args) {
        int correctAnswer = 0;
        String [][] grid = {
                {"Alabama", "California", "Delaware", "Florida", "Georgia",
                "Hawaii", "Idaho", "Kansas", "Lousiana", "Maryland", "New Mexico", "Oregon",
                "Pennsylvania", "Rhode Island", "South Carolina", "Texas", "Utah", "Virgina",
                "West Virginia"},
                {"Montgomery", "Sacramento", "Dover", "Tallahassee", "Atlanta",
                "Honolulu", "Boise", "Topeka", "Baton Rouge", "Annapolis", "San Jose", "Salem",
                "Harrisburg", "Providence", "Columbia", "Austin", "Salt Lake City", "Richmond",
                "Charleston"}};

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

            for(int k = 0; k < grid[i].length; k++ ){
                System.out.println("What is the capital of " + grid[0][k] + "?");
                String capital = input.next();
                String answer = grid[1][k];


                if(capital.equalsIgnoreCase(answer)){
                correctAnswer ++;   
                System.out.println("Your answer is correct");
                }
                else
                    System.out.println("The correct answer should be " + answer);
            }

        }
        System.out.println("The correct count is " + correctAnswer);
    }
}
2
  • That's not infinite. It's merely repeating itself because of the nesting of the loops. Commented Feb 16, 2014 at 18:35
  • The statement that prints the count is inside the loop. Commented Feb 16, 2014 at 18:35

4 Answers 4

1

It's not infinite. It just goes twice:

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

There's no need for this i variable here, and hence the entire outer-loop as well, since you use both sub-arrays, grid[0] and grid[1] on the first go-through

Sign up to request clarification or add additional context in comments.

Comments

1

Try removing the extraneous for loop:

public class exercise09_17 {

    static Scanner input = new Scanner(System.in).useDelimiter("\r\n"); 
    public static void main(String[] args) {
        int correctAnswer = 0;
        String [][] grid = {
                {"Alabama", "California", "Delaware", "Florida", "Georgia",
                "Hawaii", "Idaho", "Kansas", "Lousiana", "Maryland", "New Mexico", "Oregon",
                "Pennsylvania", "Rhode Island", "South Carolina", "Texas", "Utah", "Virgina",
                "West Virginia"},
                {"Montgomery", "Sacramento", "Dover", "Tallahassee", "Atlanta",
                "Honolulu", "Boise", "Topeka", "Baton Rouge", "Annapolis", "San Jose", "Salem",
                "Harrisburg", "Providence", "Columbia", "Austin", "Salt Lake City", "Richmond",
                "Charleston"}};



        for(int k = 0; k < grid[0].length; k++ ){
            System.out.println("What is the capital of " + grid[0][k] + "?");
            String capital = input.next();
            String answer = grid[1][k];


            if(capital.equalsIgnoreCase(answer)){
                correctAnswer ++;   
                System.out.println("Your answer is correct");
            } else
                System.out.println("The correct answer should be " + answer);
        }

        System.out.println("The correct count is " + correctAnswer);
    }
}

1 Comment

Your code snippet won't compile because there is no i variable, the for loop should have k < grid[0].length
0

Change

System.out.println("What is the capital of " + grid[0][k] + "?");

to

System.out.println("What is the capital of " + grid[i][k] + "?");

Since you are using grid[0][k] after going through all k iterations it will come back for another k iteration on same i=0 till i<grid.length

Comments

0

You do not have an infinite loop, but you will repeat the process as many times as the grid length. Basically, you do not need the outer for loop. Remove the loop

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

and instead of

for(int k = 0; k < grid[i].length; k++ ){

change to

for(int k = 0; k < grid[0].length; k++ ){

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.