Validating Input Using Java.Util.Scanner

Integer validation using java.util.Scanner

It seems your solution is already quite simple. Here is a more minimalist version:

System.out.print("Please enter an integer: ");
while(!scan.hasNextInt()) scan.next();
int demoInt = scan.nextInt();

from https://stackoverflow.com/a/23839837/2746170

Though you would only be reducing lines of code while possibly also reducing readability.

How to use java scanner with string validation?

When sc.hasNext("[A-Za-z]*") returns true, that means the next input you read will be the one you want. So you need to read f_name in after the loop ends.

You still need sc.next() inside the loop to move past bad input; otherwise you will have an infinite loop.

By the way, perhaps you want to use + instead of * in your regular expression. * means "zero or more", and + means "one or more". I assume you want one or more characters to be input.

while (!sc.hasNext("[A-Za-z]+")) {
System.out.println(ANSI_RED + " ERROR: Invalid option!");
System.out.print(ANSI_RESET + " Type your name here (use only latin characters) > ");
sc.next();
}

String f_name = sc.next().toUpperCase();

How to validate that input to Scanner is an int?

What you are doing with sc.nextInt() will only allow the user to enter an int or the program will throw an InputMismatchException (thus that part behaves the way you want). If you want to make sure the number isn't negative though, do this:

System.out.println("Enter your age here:");
while (!sc.hasNextInt()) {
System.out.println("Please enter an integer.");
sc.next();
}

int age = sc.nextInt();

if(age < 0) {
//do what you want if the number is negative
//if you're in a loop at this part of the program,
//you can use the continue keyword to jump back to the beginning of the loop and
//have the user input their age again.
//Just prompt them with a message like "invalid number entered try again" or something to that affect
}
else {
setAge(age);
//continue execution
}

Java scanner input validation

Your loop logic

do {
...
} while (number == 1 || number == 2 || number == 3);

requires staying in the loop for as long as the answer is valid. You want to invert your condition:

do {
...
} while (!(number == 1 || number == 2 || number == 3));

or use De Morgan's Law to invert individual components:

do {
...
} while (number != 1 && number != 2 && number != 3);

In addition, when Scanner's hasNextInt returns false, you need to take the invalid input off the scanner, for example with nextLine that you ignore. Otherwise you get an infinite loop:

while (!scanner.hasNextInt()) {
System.out.println("Invalid input!");
scanner.nextLine(); // This input is ignored
}

Java check if a scan input is both a specific string and only three digits

That looks like a regular expression to me. You can use a Pattern and Matcher to test if the given order matches the Pattern; does it start with F or S then B and then three digits. Like,

String[] arr = { "SB123", "FB124", "CBXXX", "FB1234" };
Pattern p = Pattern.compile("[SF]B\\d{3}");
for (String s : arr) {
Matcher m = p.matcher(s);
System.out.printf("%s %b%n", s, m.matches());
}

Outputs

SB123 true
FB124 true
CBXXX false
FB1234 false


Related Topics



Leave a reply



Submit