In the code below, when I input anything other than an integer value the code does not ask for my input again and just loops the string outputs infinitely. A little help...
int choice = 0;
while(choice == 0)
{
try
{
System.out.println("Start by typing the choice number from above and hitting enter: ");
choice = input.nextInt();
}
catch(Exception e)
{
}
if ((choice == 1) || (choice == 2) || (choice == 3))
{
break;
}
else
{
System.out.println("Invalid choice number. Please carefully type correct option.");
choice = 0;
}
}
When you input a non-integer it will not be consumed. You need to scan past it. This can be done by, for example, adding a input.nextLine() statement to your catch block. This will consume the invalid input and allow your program to read new data.
This will solve your problem:
catch(Exception e)
{
input.nextLine(); // Consume the invalid line
System.out.println("Invalid choice number. Please carefully type correct option.");
}
You could also read the line as a string and try to parse it as a number using Scanner.nextLine and Integer.parseInt, but I prefer using nextInt for integers. It makes the purpose of the code more clear (in my opinion).
When nextInt is used and the next input is not an int, it will throw an exception but not consume the data, i.e. the next call will return immediately because the data is still present.
You can fix this by calling the skip method with a pattern like [^0-9]* to skip all invalid input. Then an input like "aaa3" would work. If you want to ignore everything, use .* as pattern.
The trouble is that you are not consuming the remaining data in the stream. I solved it with the following code, although you will want to document you code better before you use it in a program:
int choice = 0;
while(choice == 0)
{
try
{
System.out.print("Start by typing the choice number from above and hitting enter: ");
choice = input.nextInt();
}
catch(Exception e)
{
input.next();
System.out.println("Invalid choice number. Please carefully type correct option.");
}
if ((choice == 1) || (choice == 2) || (choice == 3))
{
break;
}
choice = 0;
}
You can simplify and reduce your code as follows:
int choice;
System.out.println("Start by typing the choice number from above and hitting enter: ");
while(true)
{
try {
choice = input.nextInt();
if ((choice == 1) || (choice == 2) || (choice == 3))
break;
} catch(InputMismatchException e) { // handle only the specific exception
input.nextLine(); // clear the input
}
System.out.println("Invalid choice number. Please carefully type correct option.");
}
Are you using Scanner(system.in); from the import java.util.Scanner; package?
Try adding input.nextLine(); in the catch to clear the value to stop the infinite loop.
public static void main(String[] args) {
int choice = 0;
Scanner input = new Scanner(System.in);
while(choice == 0)
{
try
{
System.out.println("Start by typing the choice number from above and hitting enter: ");
choice = input.nextInt();
}
catch(Exception e)
{
input.nextLine();
System.out.println("Invalid choice number. Please carefully type correct option.");
}
if ((choice == 1) || (choice == 2) || (choice == 3))
{
break;
}
else
{
System.out.println("Invalid choice number. Please carefully type correct option.");
choice = 0;
}
}
}
Looks like in the line choice = input.nextInt();
choice value is always 0. Print choice soon after that.
Also for non integer value add a condition to break from the loop.
Related
New to Java here. I made the following simple code that asks the user to choose between option 1 or 2. If the selected option is 1, then it should print "You said hi", which it works well, also if selected option is 2, it should prints "you said goodbye" which it doesn't, Am I missing something here? Maybe the If statement is wrong?
the Code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please type 1 to say hi");
System.out.println("Please type 2 to say goodbye");
if (input.nextInt() == 1) {
System.out.println("You said hi");
} else if (input.nextInt() == 2) {
System.out.println("you said goodbye");
}
}
Every time you call nextInt() it stops to wait for an int. Here you want to compare a single int from the user with one or two. Save the int you get from the user. Like,
int v = input.nextInt();
if (v == 1) {
System.out.println("You said hi");
} else if (v == 2) {
System.out.println("you said goodbye");
}
Every time you call nextInt() on your scanner, input is being consumed. So in your case, when the else if condition is checked, the next input is consumed, not the previous one compared. You need to cache your scanner's state:
int answer = input.nextInt();
if (answer == 1) {
System.out.println("You said hi");
} else if (answer == 2) {
System.out.println("you said goodbye")
}
For your specific case, converting to a switch statement would be another option, which evaluates its operand only once:
switch (input.nextInt()) {
case 1:
System.out.println("You said hi");
break;
case 2:
System.out.println("you said goodbye")
break;
}
Every call to input.nextInt() will wait for the new key from input(here it is the user input).
the input.nextInt() == 1 will wait for the user input.
If it is validates to true, the thread will successfully execute System.out.println("You said hi").
Else if it validates to false, it will execute the condition in the input.nextInt() ==2 where the thread will keep waiting for the next input from the user because of the input.nextInt().
If you wish to get input from user only once, execute input.nextInt() only once and store it in a variable and run cases against it. Like,
// input from user
int selection = input.getInt();
if (selection == 1) {
System.out.println("the user entered 1");
}
else if (selection == 2) {
System.out.println("the user entered 2");
}
Please get input first then check it. (Don't get input in condition statement).
Your edited code is as follows:
Added one line (int selectedOption = input.nextInt())
edit condition statement (selectedOption == 1 and selectedOption == 2)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please type 1 to say hi");
System.out.println("Please type 2 to say goodbye");
//Get input
int selectedOption = input.nextInt() ;
//Check
if (selectedOption == 1) {
System.out.println("You said hi");
} else if (selectedOption == 2) {
System.out.println("you said goodbye");
}
}
I'm having some trouble with a menu program I am writing for my java class. After one program is run, when the program goes to do a second loop it throws a NoSuchElementException on the line where it is supposed to take the user's input for the next program they want to run. I'm assuming it has something to do with the scanner getting messed up but I can't find the issue. Anyone have any ideas?
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
String pin;
int selection = 0;
boolean valid = false;
do {
System.out.print("Please enter the password: ");
pin = console.nextLine();
valid = checkPassword(pin);
} while (!valid);
while (selection != 4 && valid == true) {
System.out.printf("%nPlease select a number from the menu below %n1: Wage "
+ "Calculator 2: Tip Calculator 3: Grocery Discount 4: Exit %n");
selection = console.nextInt();
if (selection == 1) {
calc_wages();
} else if (selection == 2) {
calc_tip();
} else if (selection == 3) {
System.out.print("We haven't gotten this far yet");
} else if (selection == 4){
System.out.print("Thank you for using the program.");
break;
} else {
System.out.print("There is no option for what you entered. Try again");
}
selection = 0;
}
}//main
Your code so far is fine.
From what you're saying the problem starts after the user makes a selection.
In calc_wages() and/or calc_tip() it's possible that you use another Scanner object to get the user's input.
This is a source of problems.
Declare 1 Scanner object at the class level and use it throughout you code and close it only when it is no longer needed.
Hey guys I'm trying to create a loop until a correct character choice is entered by the user. When I enter a wrong choice I get the error java.lang.NullPointerException. It might be with the way I'm inputing but I don't want to change that if I don't have to. choice is a private member of the class.
char wf() {
Scanner input = new Scanner(System.in);
System.out.println("What is your choice? (x/o)");
choice = input.findInLine(".").charAt(0);
while (choice != 'x' && choice != 'o') {
System.out.println("You must enter x or o!");
choice = input.findInLine(".").charAt(0);
}
return choice;
}//end wf
Change the function as below (I have tested this code):
char wf() {
Scanner input = new Scanner(System.in);
System.out.println("What is your choice? (x/o)");
char choice = input.findInLine(".").charAt(0);
while (choice != 'x' && choice != 'o') {
System.out.println("You must enter x or o!");
choice = input.next().charAt(0);
}
return choice;
}//end wf
Check input.findInLine(".") to see if it null. If you don't have the expected input, it won't return anything..
change your code like below
char wf() {
Scanner input = new Scanner(System.in);
System.out.println("What is your choice? (x/o)");
if(input.findInLine(".") !=null){
choice = input.findInLine(".").charAt(0);
while (choice != 'x' && choice != 'o') {
System.out.println("You must enter x or o!");
choice = input.findInLine(".").charAt(0);
}
}
return choice;
}//end wf
I have this piece of code:
do {
try {
input = sc.nextInt();
}
catch(Exception e) {
System.out.println("Wrong input");
sc.nextLine();
}
}
while (input < 1 || input > 4);
Right now, if I input 'abcd' instead of integer 1-4, it gives message "Wrong Input" and the program loops, how can I make it so that it also gives "Wrong Input" when I entered integer that doesn't fulfill the boolean (input < 1 || input >4)?
So that if I entered 5, it will also give me "Wrong Input".
Add this:
if(input < 1 || input > 4) {
System.out.println("Wrong input");
}
after input = sc.nextInt();
As of now, your try-catch block is checking if input is an int type. The do-while loop is checking input after it has been entered, so it is useless. The condition must be checked after the user enters what he/she wants. This should fix it:
do
{
try
{
input = sc.nextInt();
if(input < 1 || input > 4) // check condition here.
{
System.out.println("Wrong input");
}
}
catch(Exception e)
{
System.out.println("Expected input to be an int. Try again."); // tell user that input must be an integer.
sc.nextLine();
}
} while (input < 1 || input > 4);
You can also do this:
while (true) {
try {
input = sc.nextInt();
if (input >= 1 && input <= 4) {
break;
}
} catch (Exception e) {
System.out.println("Wrong input");
}
sc.nextLine();
}
I have the following method
public static int modeChooser(){
int choice = 0;
Scanner kb = new Scanner(System.in);
while(choice == 0){
try {
choice = kb.nextInt();
} catch (Exception e) {
continue;
}
if(choice < 1 || choice > 5){
continue;
}
}
return choice;
}
The goal is to only allow the user to put in 1,2,3,4, or 5;
If the user types a string or a too high/low number, the method should just restart until i have the proper int.
Here is an example for the flow:
User types: 1 -> all ok
User types: saddj -> method restarts -> user types 3 --> all ok
Any ideas?
Change to:
do {
// read choice value
if (choice < 1 || choice > 5) {
// hint to user
}
} while(choice < 1 || choice > 5);
I think you can simply put your check in the while condition itself as below:
while(choice < 1 || choice > 5){
try {
choice = kb.nextInt();
} catch (Exception e) {
//ignore the exception and continue
}
}
This way actually works fine:
public static int modeChooser(){
int choice = 0;
Scanner kb = new Scanner(System.in);
while(choice == 0){
try {
choice = kb.nextInt();
} catch (Exception e) {
System.out.println("Sorry but you have to enter 1,2,3,4, or 5! Please try again.");
choice = modeChooser();
}
}
if(choice < 1 || choice > 5){
System.out.println("Sorry but you have to enter 1,2,3,4, or 5! Please try again.");
choice = modeChooser();
}
return choice;
}
if(choice >= 1 && choice <= 5)
break;
else
choice = 0;
If kb.NextInt() fails the data in the input stream remains, you need to skip past it. If you don't skip the invalid data the loop will continuously try, and fail, to read the invalid input resulting in an infinite loop.
You can use kb.next() to skip over the invalid input:
while (true)
{
try
{
choice = kb.nextInt();
if(choice >= 1 && choice <= 5) break;
}
catch (InputMismatchException e)
{
e.printStackTrace();
kb.next();
}
}
I think it's better to use the Scanner.nextLine() and Integer.parseInt() methods:
while(choice < 1 || choice > 5){
try {
choice = Integer.parseInt(kb.nextLine());
} catch (Exception e) {
//...print error msg
}
}
You could include your condition on choice directly in the while condition:
while(choice < 1 || choice > 5){
try {
choice = kb.nextInt();
} catch (Exception e) {
continue;
}
}
(In your current code, is the user enters 7, choice takes that value, the while condition becomes false and your method returns 7, which it should not).
And instead of catching an exception, you could use the hasNextInt() method to make the code cleaner:
public static int modeChooser() {
int choice = 0;
Scanner kb = new Scanner(System.in);
while (choice < 1 || choice > 5) {
if (!kb.hasNextInt()) {
kb.next();
} else {
choice = kb.nextInt();
}
}
return choice;
}
If you do want to use a recursive method, it could look like:
public static int modeChooser() {
Scanner kb = new Scanner(System.in);
while (!kb.hasNextInt()) {
kb.next();
}
int choice = kb.nextInt();
return (choice >= 1 && choice <= 5) ? choice : modeChooser();
}