Reputation: 69
The program is supposed to work as follows, the program asks for a number and the user must enter it, in case the user enters a letter, a number less than 0 or does not enter anything, an error will appear and it will tell the user to try again, giving the user infinite attempts.
I have tried but I only get an infinite loop inside the catch, I would appreciate any help.
public static void numbers(){
Scanner in = new Scanner(System.in);
int number;
System.out.print("Enter only numbers: ");
while(true){
try {
number = in.nextInt();
if(number > 0){
break;
} else {
System.out.print("Try again: ");
}
} catch (Exception e){
System.out.print("Try again: ");
}
}
Upvotes: 1
Views: 1116
Reputation: 125
Here is the answer to your question:
import java.util.Scanner;
class test {
public static void numbers(){
while(true){
Scanner userInteraction = new Scanner(System.in);
System.out.print("Enter only numbers: ");
String userInput = userInteraction.nextLine();
if (userInput.matches("\\d+$")){
double userNum = Integer.parseInt(userInput);
System.out.println("Congrats! You typed a number!");
break;
} else {
System.out.println("GRRR! You didn't type a number!");
continue;
}
}
}
public static void main(String[] args) {
numbers();
}
}
Output:
How it works:
In order to check if the userInput is a number, you need to use a regex (or a REGular EXpression). I used the regex matcher \\d+$
. More info about it here and here! After verifying that the userInput was a number(with the regex matcher), I used the function Integer.parseInt(userInput)
to change the String into an Integer. Then, I will congratulate the player and break;
the loop. If the player fails to type a number, I will warn the player and continue;
the loop.
Upvotes: 0
Reputation: 844
Scanner sc = new Scanner(System.in);
int n = 0;
while (n == 0) {
String s = sc.nextLine();
try {
n = Integer.parseInt(s);
}
catch(Exception e) {
System.out.println("Try again");
}
}
Upvotes: 1
Reputation: 21
public static void main(String[] args) throws IOException{
BufferedReader var = new BufferedReader(new InputStreamReader(System.in));
boolean response = false;
while (!response) {
try {
System.out.println("Input number: ");
String input = var.readLine();
int n = Integer.parseInt(input);
response = true;
} catch (Exception e) {
response = false;
}
}
}
Upvotes: 2
Reputation: 504
I think that the problem is that if the input is not actually an integer, it stays in the scanner. Scanner does not advance at all to the next line or something.
So this invalid integer is blocking you from accessing something more, even if it were a number.
From the java doc:
This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.
So for unsuccessful translation, the problematic line STAYS, and next call to the nextInt method will fail as well, trying the parse the problematic line. Thus your infinite loop.
Upvotes: 1
Reputation: 467
Well first of all, this is not the purpose of stackoverflow. But i would help you.
If you compare with other cmd processes always they ask for a y/N answer. So:
public static void numbers(Scanner overrideIn = null){
Scanner in = overrideIn ?? new Scanner(System.in);
int number;
System.out.print("Enter only numbers: ");
while(true){
try {
number = in.nextInt();
if(number > 0){
break;
} else {
System.out.print("Try again: ");
this.TryAgain(in);
}
} catch (Exception e){
System.out.print("Try again: ");
this.TryAgain(in);
}
}
public static void TryAgain(Scanner in)
{
char respond = in.next().charAt(0);
if (respond == 'y')
{
this.numbers(in);
} else {
System.out.print("See ya!");
}
}
Upvotes: 1