I have one problem. Neither of these work for my code.
When running this code with
sodaType = keyboard.next();
userInput (called sodaType in code) only saves the first part of "Root Beer", outputting ("Root").
I googled the problem and
sodaType = keyboard.nextLine();
Allows for "white space", but skips the userInput, outputting nothing, skipping the if statement.
I found different answers on this site
- How do I make Java register a string input with spaces?
- User Input not working with keyboard.nextLine() and String (Java)
- Scanner doesn't see after space
I am confused on why the nextLine() worked for them, and how I should continue.
while(true) {
System.out.println("Please enter a brand of soda. ");
System.out.print("You can choose from Pepsi, Coke, Dr. Pepper, or Root Beer: ");
sodaType = keyboard.next();
System.out.println("sodatype" + sodaType);
if (sodaType.equalsIgnoreCase("pepsi") || sodaType.equalsIgnoreCase("coke") ||
sodaType.equalsIgnoreCase("dr pepper") || sodaType.equalsIgnoreCase("dr. pepper") ||
sodaType.equalsIgnoreCase("root beer"))
{
System.out.println("you chose " + sodaType);
break;
}
else {
System.out.println("Please enter an avaiable brand of soda. ");
}
}