Reputation: 23
I am completely new to programming, and I am trying to write a very simple program in Java. I want the user to be given a prompt, be given two options for what to do, and then select one of the two choices by typing a specific word, which will then print out a specified message.
Code below:
import java.util.Scanner;
public class coffeeProgram {
public static void main(String[] args) {
String Quit;
String Coffee;
Quit = "quit";
Coffee = "coffee";
System.out.println("You are sitting at your cubicle, staring at your computer screen.");
System.out.println("What do you want to do?");
System.out.println("Options: a) type 'coffee' to get a cup of coffee; or b) type 'quit' to quit your job");
System.out.println("Enter an action:");
Scanner input1 = new Scanner(System.in);
String coffee = input1.next();
String quit = input1.next();
if (coffee.equals(Coffee)) {
System.out.println("You stand up, and start walking to the coffee machine.");
}
else if (quit.equals(Quit)) {
System.out.println("You walk into your boss's office and say I QUIT");
}
else {
System.out.println("That's not a valid command");
}
input1.close();
}
}
The issue I am having is that, after the initial prompt prints out, the program doesn't seem to acknowledge user input unless you enter the required word, hit return, and then some other character followed by return. For instance, the word of one choice is "coffee", but if I input "coffee" followed by the return key, nothing happens unless I enter some character followed by return again.
Based on the research I have done, I think my issue has something to do with scanner not recognizing the return key after the user enters the required word. Why doesn't this work?
Upvotes: 0
Views: 455
Reputation: 65
This is because you used input1.next(); twice.
//create Scanner instance
Scanner input1 = new Scanner(System.in);
//wait NEXT input
String coffee = input1.next();
//wait another NEXT input
String quit = input1.next();
The program is waiting for two inputs before it can give you some response.
So, the proper way should be like this.
//create Scanner instance
Scanner input1 = new Scanner(System.in);
//wait NEXT one input
String command = input1.next();
//compare input with predefined commands
if(command.equals(Coffee)){
}else if(command.equals(Quit)){
}else{
}
Upvotes: 1
Reputation: 1
I've think you should define one String parameter.
then you should use nextLine()
for get String like this
String choices = input1.nextLine();
and then do if...else
Upvotes: 0
Reputation: 26
Your problem is you're assigning two variables to the one line of input, coffee and quit. So you just need to get rid of the String quit = input1.next();
line and change else if (quite.equals(Quit)) {
to else if (coffee.equals(Quit)) {
. All this means is that your input is in coffee, it doesn't matter what the input you gather from them, it just needs to be in one variable and then you can use that variable to see if it equals quit, coffee or if its invalid.
Hope I've helped you understand it a bit better.
Upvotes: 0
Reputation: 4587
The issue is on these lines
String coffee = input1.next();
String quit = input1.next();
Your code is expecting two inputs. Instead of reading coffee
and quit
as two separate variables, you should be reading the choice and then do if..else
.
Something like
String choice = input1.next();
if (choice.equals(Coffee)) {
System.out.println("You stand up, and start walking to the coffee machine.");
}
else if (choice.equals(Quit)) {
System.out.println("You walk into your boss's office and say I QUIT");
}
Upvotes: 1