Java line prints itself before while (true) loop is entered

In my java code below:

while(true) {
        userResponse = keyboard.nextLine();
        if(userResponse.length() == 1 && userResponse.charAt(0) == 'y') {
            break;
        }
        else if(userResponse.length() == 1 && userResponse.charAt(0) == 'n') {
            System.out.println("Come back next time " + userName + ".");
            System.exit(0);
        }
        else {
            System.out.println("Invalid response.");
        }
    }

      

Before the loop is injected into the code block:

else {
            System.out.println("Invalid response.");
        }

      

Performed

... Can anyone point out why this is happening or what is wrong?

Edit: The keyboard scanner is used in this code block as well.

while(true) {
        userResponse = keyboard.nextLine();
        if(userResponse.length() == 1 && userResponse.charAt(0) == 'y') {
            System.out.println("Great! Let get started.");
            break;
        }
        else if(userResponse.length() == 1 && userResponse.charAt(0) == 'n') {
            System.out.println("Come back next time " + userName + ".");
            System.exit(0);
        }
        else {
            System.out.println("Invalid response.");
        }
    }

      

Thanks for the answers, I fixed it by replacing "keyboard.nextLine ();" with "keyboard.next ();"

+3


source to share


3 answers


This could be one of the reasons that happen to your code. You already took UserInput from the keyboard object of the scanner class, so it gives the else answer. This especially happens if you take a non-String object from it

public class Test {

    public static void main(String[] args) {
        Scanner keyboard= new Scanner(System.in);
        int n=keyboard.nextInt();

        String userResponse;
        while(true) {
            userResponse = keyboard.nextLine();
            if(userResponse.length() == 1 && userResponse.charAt(0) == 'y') {
                System.out.println("Great! Let get started.");
                break;
            }
            else if(userResponse.length() == 1 && userResponse.charAt(0) == 'n') {
                System.out.println("Come back next time " + "" + ".");
                System.exit(0);
            }
            else {
                System.out.println("Invalid response.");
            }
        }
    }

} 

      

Output

5
Invalid response.

      

now change the structure of your code to get a String Input from this Scanner object and not get another type of data types that the code is running.

With String as previous input

public class Test {

    public static void main(String[] args) {
        Scanner keyboard= new Scanner(System.in);
        String n=keyboard.nextLine();
        String userResponse;
        while(true) {
            userResponse = keyboard.nextLine();
            if(userResponse.length() == 1 && userResponse.charAt(0) == 'y') {
                System.out.println("Great! Let get started.");
                break;
            }
            else if(userResponse.length() == 1 && userResponse.charAt(0) == 'n') {
                System.out.println("Come back next time " + "" + ".");
                System.exit(0);
            }
            else {
                System.out.println("Invalid response.");
            }
        }
    }

}

      



Output

j
y
Great! Let get started.

      

Without any previous answer with this object, your code will work.

public class Test {

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

        String userResponse;
        while(true) {
            userResponse = keyboard.nextLine();
            if(userResponse.length() == 1 && userResponse.charAt(0) == 'y') {
                System.out.println("Great! Let get started.");
                break;
            }
            else if(userResponse.length() == 1 && userResponse.charAt(0) == 'n') {
                System.out.println("Come back next time " + "" + ".");
                System.exit(0);
            }
            else {
                System.out.println("Invalid response.");
            }
        }
    }

}

      

and gives me the desired output

y
Great! Let get started.

      

I used to do this all the time, creating two OBJECT's Scanner Class to get String Input and others to get other data types. Input (Too frankly, even I couldn't figure out why I need to create two objects to get string types and other data in java without any errors. If anyone knows, please let me know)

+1


source


In my opinion the Boolean variable is set to false and the first user of characters does not enter 'n' - hence the else-if is executed.



+1


source


I don't know what your code looks like.

Given the attached sample, I wrote the code below which doesn't enter the else block first

 else {
        System.out.println("Invalid response.");
      }

      

Please validate your code below and respond!

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        String userResponse = "";
        Scanner keyboard = new Scanner(System.in);
        while (true) {
            System.out.println("Please Enter Your Input");
            userResponse = keyboard.nextLine();
            if (userResponse.length() == 1 && userResponse.charAt(0) == 'y') {
                System.out.println("Great! Let get started.");
                break;
            } else if (userResponse.length() == 1
                    && userResponse.charAt(0) == 'n') {
                System.out.println("Come back next time " + userResponse + ".");
                System.exit(0);
            } else {
                System.out.println("Invalid response.");
            }
        }
    }
}

      

Output:

Please Enter Your Input
yes
Invalid response.
Please Enter Your Input
no
Invalid response.
Please Enter Your Input
y
Great! Let get started.

      

It would be nice if you could post all the code! If you still face the problem!

+1


source







All Articles