简体   繁体   中英

How to get the Java Scanner utility to input twice

so heres my code


public class Practice01{

    public static void main (String[] args){
        
        
        System.out.println("Hi there");

          Scanner scr = new Scanner(System.in);

          String response = scr.nextLine();
          
          
         if (response.equalsIgnoreCase("hello") || response.equalsIgnoreCase("hi") || response.equalsIgnoreCase("hey")) {
             System.out.println("Oh, well arent you well-mannered. Hello there, how are you?");}
             
         
             else {
                 System.out.println("Invalid Input");
                 
                 
                 String responseG;
                 responseG = scr.nextLine();
             
             if (responseG.equalsIgnoreCase("good")) {
                 System.out.println("Glad to hear");
                 
             }
         }     
        }
    }  

im a bit of a noobie when it comes to java, I just started today, but after the else statment here, java terminates itself for some reason, and it just doesnt care about the rest of the code. I was looking online and I saw that if you wanted to take another input you used the .nextLine(); function (i dont think its called a function but you know what I mean) but after I type either hey, hello, or hi, it prints "Oh, well arent you well-mannered. Hello there, how are you?" and then I cant type anything else, and it says < terminated > . can anyone help? thanks

Edit: Apparently I'm supposed to move the "responseG" variable and next line into the if statment. When I do that it doesnt activate, (using eclipse IDE, and it just appears as white and as an error) and tells me to delete else. https://gyazo.com/1a27fa9ab8802d594cccb35ecc0cb663 picture of what happens. furthermore if i try to use an else if statment it also says to delete it

You have to use a loop to keep the program working. After you get "Oh, well aren't you well-mannered. Hello there, how are you?" printed, it terminates the fact that there is no more tasks to do.

Hi, welcome to Stackoverflow!

Your program will only ask for another input from your keyboard if you don't type "hello" or "Hello", or "hey", you know what I mean. If you type "hello" then it will print a line with

"Oh, well aren't you well-mannered. Hello there, how are you?"

And the program will be terminated...

That's what your code it's telling your program to do , since you are only asking for another input in the else statement's body.

As I can see you don't want to use a for loop, since you seem to only care about a path that says hi... how are you?... good... glad to hear... but if you care for it, you can use a while() statement or a do..while() or a for() to make it save in the variable many responses with the scr.nextLine(); function (it is a function), in that case you have to define when/how the program is going to stop asking for input and will terminate.


I believe this is what does what you were trying to do, with the proper indentation, and not declaring unnecessary variables:

public class Practice01{
    public static void main (String[] args){

        System.out.println("Hi there");

        Scanner scr = new Scanner(System.in);
        String response = scr.nextLine();
        
        if (response.equalsIgnoreCase("hello") || response.equalsIgnoreCase("hi") || response.equalsIgnoreCase("hey")) {
            System.out.println("Oh, well arent you well-mannered. Hello there, how are you?");
        }     
        else {
                System.out.println("Invalid Input");
        }

        //You don't need ResponseG... you've already created the response variable
        response = scr.nextLine();
            
        if (response.equalsIgnoreCase("good")) {
                System.out.println("Glad to hear");
        }
        else {
                System.out.println("Invalid Input");
        }
    }
}

So what you want to do is move the ResponseG if statement into the your response if statement as that is stopping the code from complete since you are putting in the correct inputs. Also for future projects, to be more organized create variables at the beginning of the code.

    public class Practice01{
      public static void main (String[] args){  
        System.out.println("Hi there");

        String responseG;
        Scanner scr = new Scanner(System.in);
        String response = scr.nextLine();

        if (response.equalsIgnoreCase("hello") || 
            response.equalsIgnoreCase("hi") || 
            response.equalsIgnoreCase("hey")) {
          System.out.println("Oh, well arent you well-mannered. Hello there, how are you?");
             
          responseG = scr.nextLine();
             
          if (responseG.equalsIgnoreCase("good")) {
            System.out.println("Glad to hear");
          }
        } else {
          System.out.println("Invalid Input");
        }
      }  
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM