Reputation: 21
I am having trouble using this try/catch statement. I am trying to use it to throw an error message that says, "Please enter an integer!" if the user enters a letter. The first one I used worked, but it ends up taking 2 lines of user input rather than just one, so it essentially skips over a question that was supposed to be asked. After that, none of the other ones work at all, they just get completely skipped. I also need to do the same thing for user input where if a user enters an integer where a letter should be, it throws an error message that says "Please enter a string!". I know I am pretty close!
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean validInput = false;
int val = 0;
System.out.print("Enter your first name: ");
String firstName = input.nextLine();
System.out.print("Enter your last name: ");
String lastName = input.nextLine();
System.out.print("Enter your address: ");
String address = input.nextLine();
System.out.print("Enter your city: ");
String city = input.nextLine();
System.out.print("Enter your state: ");
String state = input.nextLine();
System.out.print("Enter your zip code + 4: ");
String zip = input.nextLine();
while(!validInput) {
try {
val = input.nextInt();
validInput = true;
} catch(Exception e) {
System.out.println("Please enter an integer!");
input.nextLine();
}
}
System.out.print("Enter amount owed: ");
String amount = input.nextLine();
while(!validInput) {
try {
val = input.nextInt();
validInput = true;
} catch(Exception e) {
System.out.println("Please enter an integer!");
input.next();
}
}
System.out.print("Enter your payment amount: ");
String payment = input.nextLine();
while(!validInput) {
try {
val = input.nextInt();
validInput = true;
} catch(Exception e) {
System.out.println("Please enter an integer!");
input.next();
}
}
System.out.print("Enter the date of payment: ");
String date = input.nextLine();
while(!validInput) {
try {
val = input.nextInt();
validInput = true;
} catch(Exception e) {
System.out.println("Please enter an integer!");
input.next();
}
}
System.out.println("\t\t\t\t\t" + "XYZ Hospital");
System.out.println();
System.out.println("Name Information" + "\t\t\t\t" + "Address" + "\t\t\t\t\t\t" + "Payment");
System.out.println();
System.out.println("Last" +"\t"+ "First" + "\t\t\t" + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t" + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount" + "\t" + "Payment Date");
System.out.println();
System.out.println(lastName + " " + firstName + "\t" + address + " " + city + ", " + state + " " + zip + "\t" + amount + "\t\t" + payment +"\t\t"+ date);
System.out.print("Would you like to enter abother patient? Type Yes or No: ");
String userInput = input.nextLine();
if (userInput.equals("Yes")) {
while (userInput.equals("Yes")) {
System.out.print("Enter your first name: ");
String firstName2 = input.nextLine();
System.out.print("Enter your last name: ");
String lastName2 = input.nextLine();
System.out.print("Enter your address: ");
String address2 = input.nextLine();
System.out.print("Enter your city: ");
String city2 = input.nextLine();
System.out.print("Enter your state: ");
String state2 = input.nextLine();
System.out.print("Enter your zip code + 4: ");
String zip2 = input.nextLine();
System.out.print("Enter amount owed: ");
String amount2 = input.nextLine();
System.out.print("Enter your payment amount: ");
String payment2 = input.nextLine();
System.out.print("Enter the date of payment: ");
String date2 = input.nextLine();
System.out.println("\t\t\t\t\t" + "XYZ Hospital");
System.out.println();
System.out.println("Name Information" + "\t\t\t\t" + "Address" + "\t\t\t\t\t\t" + "Payment");
System.out.println();
System.out.println("Last" +"\t"+ "First" + "\t\t\t" + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t" + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount" + "\t" + "Payment Date");
System.out.println();
System.out.println(lastName2 + " " + firstName2 + "\t" + address2 + " " + city2 + ", " + state2 + " " + zip2 + "\t" + amount2 + "\t\t" + payment2 +"\t\t"+ date2);
System.out.print("Would you like to enter another patient? Type Yes or No: ");
userInput = input.nextLine();
}
}
else if (userInput.equals("No")) {
System.out.println("Goodbye");
}
Upvotes: 1
Views: 70
Reputation: 647
A quick solution maybe, I've used Scanner.hasNextInt
to check what the next token is before getting required ints:
import java.util.Scanner;
public class java_so {
private static int privateGetInt(String request, Scanner input) {
// cant be false, but we return when done.
while (true) {
// print the question
System.out.println(request);
// check what the next token is, if an int we can then retrieve
if (input.hasNextInt() == true) {
int out = input.nextInt();
input.nextLine(); // clear line, as nextInt is token orientated, not line,
return out;
} else {
// else for when not an int, clear line and try again
input.nextLine();
}
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your first name: ");
System.out.println(input.nextLine());
// call function above
System.out.println(privateGetInt("enter an Int", input));
System.out.print("Enter your last name: ");
System.out.println(input.nextLine());
System.out.print("Enter your address: ");
System.out.println(input.nextLine());
}
}
It seems a bit like the problem may have been after a call to input.nextInt
you haven't got rid of the rest of the line (which is probably just "\n"
) so the next call to input.getLine()
only gets that, jumping on one. Following input.getInt
with input.getLine
clears this.
Upvotes: 0
Reputation: 467
You should take input in catch block comment input.nextLine()
in catch block then it should work properly
I have added explanation in the code itself
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean validInput = false;
int val = 0;
System.out.print("Enter your first name: ");
String firstName = input.nextLine();
System.out.print("Enter your last name: ");
String lastName = input.nextLine();
System.out.print("Enter your address: ");
String address = input.nextLine();
System.out.print("Enter your city: ");
String city = input.nextLine();
System.out.print("Enter your state: ");
String state = input.nextLine();
System.out.print("Enter your zip code + 4: ");
String zip = input.nextLine();
while (!validInput) {
try {
val = input.nextInt();
validInput = true; //if exception occurs this line wont be executed
} catch (Exception e) {
System.out.println("Please enter an integer!");
// input.nextLine(); --remove this line
}
}
System.out.print("Enter amount owed: ");
String amount = input.nextLine();
//reset the value of validInput
//validInput will true after the execution of above while loop
validInput = false;
while (!validInput) {
try {
val = input.nextInt();
validInput = true; //if exception occurs this line wont be executed
} catch (Exception e) {
System.out.println("Please enter an integer!");
// input.next(); -- remove this line
}
}
}
Upvotes: 1
Reputation: 113
You don't reset validInput to False after your first while loop. So it doesn't enter the next.
Upvotes: 1