-1

So, I have a loop that uses the Scanner to input 3 separate pieces of user input, 2 ints and a String. The ints work just fine, but the String input is not working. It skips over the iput for the String, and just loops around to ask for the int again.

for(int e = 10; e > 5; e++)
    {
        System.out.println("Enter the Y coordinate of your guess. The upper left corner is 0, and the lower left is 7");
        int guessY = inputDevice.nextInt();
        System.out.println("Enter the X coordinate of your guess. The upper left corner is 0, and the upper right is 7");
        int guessX = inputDevice.nextInt();

        display[guessX][guessY] = map[guessX][guessY];
        for(int j = 0; j < 8; j++)

                {

            for(int t = 0; t < 8; t++)

            {

                System.out.print(display[j][t] + " ");

            }

            System.out.println();

        }


        System.out.println("Enter go when you are ready to make another move.");
        System.out.println("Enter quit to give up");
        String cont = inputDevice.nextLine();

        if(cont.equalsIgnoreCase("quit"))
        {
            System.out.println("Weenie.");
            System.exit(0);
        } else if(cont.equalsIgnoreCase("go")) {
            for(int i = 0; i < 100; i++)
                System.out.println("\b");
        }

        if(map[guessX][guessY] == '0')
        {
            if(map[guessX - 1][guessY - 1] == '0')
                display[guessX - 1][guessY - 1] = map[guessX - 1][guessY - 1];
            if(map[guessX - 1][guessY + 0] == '0')
                display[guessX - 1][guessY + 0] = map[guessX - 1][guessY + 0];
            if(map[guessX - 1][guessY + 1] == '0')
                display[guessX - 1][guessY + 1] = map[guessX - 1][guessY + 1];
            if(map[guessX + 0][guessY - 1] == '0')
                display[guessX + 0][guessY - 1] = map[guessX + 0][guessY - 1];
            if(map[guessX + 0][guessY + 1] == '0')
                display[guessX + 0][guessY + 1] = map[guessX + 0][guessY + 1];
            if(map[guessX + 1][guessY - 1] == '0')
                display[guessX + 1][guessY - 1] = map[guessX + 1][guessY - 1];
            if(map[guessX + 1][guessY + 0] == '0')
                display[guessX + 1][guessY + 0] = map[guessX + 1][guessY + 0];
            if(map[guessX + 1][guessY + 1] == '0')
                display[guessX + 1][guessY + 1] = map[guessX + 1][guessY + 1];
        }
        if(display[guessX][guessY] == 'M')
        {
            System.out.println("You hit a mine");
            System.out.println("Game over.");
            System.exit(0);
        }
    }
John Woo
  • 258,903
  • 69
  • 498
  • 492
Jon Takagi
  • 55
  • 3
  • 8

1 Answers1

2

From java 7 documentation http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html :

public String nextLine()
Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

You probably wanted to use:

public String next()
Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.

Community
  • 1
  • 1
tb-
  • 1,240
  • 7
  • 10