Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java line prints itself before its while(true) loop is entered [closed]

Tags:

java

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 entered the block of code:

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

is executed. Can someone point out why this is happening or whats wrong?

Edit: The keyboard Scanner is used before in this block of code as well

while(true) {
        userResponse = keyboard.nextLine();
        if(userResponse.length() == 1 && userResponse.charAt(0) == 'y') {
            System.out.println("Great! Let's 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 replies, I fixed it by replacing "keyboard.nextLine();" with "keyboard.next();"

like image 887
Marcel Avatar asked Jan 30 '26 01:01

Marcel


2 Answers

This might be one of the reason that is happening with your code . You have already taken a UserInput from the keyboard object of the scanner class that's why it's giving the else response. This particularly happens when you take other than String input from that object

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's 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 code structure to get String Input from that scanner Object and not get another kind of data types the code works.

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's 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's get started.

Without any previous response with that 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's 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's get started.

I usually have been doing this whole time creating two OBJECT of Scanner Class one to get String Input and other to get other data types Input (Too be frank even i have been not able to figure out why i needed to create two Object's for receiving String and Other data types in java without any error. If anyone know please let me know )

like image 193
Ankur Anand Avatar answered Feb 01 '26 14:02

Ankur Anand


In my opinion, the Boolean variable is having value as false, and first character user is entering is not 'n' - hence the else-if block is getting executed.

like image 23
Raúl Avatar answered Feb 01 '26 14:02

Raúl