3

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();"

Marcel
  • 149
  • 4
  • 16

3 Answers3

1

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.

Raúl
  • 1,512
  • 4
  • 23
  • 37
1

I don't know how your code look like.

Considering the sample attached i had written the below code which doesn't enter the else block first

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

Please verify your code with the below and comment back!

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

It would be good if you can post the whole code! If you are still facing the issue!

09Q71AO534
  • 3,820
  • 12
  • 37
  • 67
1

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 )

Ankur Anand
  • 3,699
  • 1
  • 21
  • 39
  • 1
    *"This is what happening with your code."*, well, no, this is just what you think it happens there. You should explain, that this _might_ be the problem. And you don't need two `Scanner` instances. Read this for more information: [Skipping nextLine() after use next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/q/13102045) – Tom Apr 22 '15 at 08:49
  • 1
    @Tom Edited the "Poor Choice of Words". :) – Ankur Anand Apr 22 '15 at 09:22