100); System.out.println("You entered " + age);"> 100); System.out.println("You entered " + age);">
Download presentation
Presentation is loading. Please wait.
Published byBenny Budiman Modified over 5 years ago
1
Validation We have covered one way to get user input, using Scanner to get it from the console We will soon cover at least one additional way to get input We have noted that users are human beings and accordingly are impossible to predict Input is not always usable
2
Validation Screen input for problems before using it
while and do… while loops are very handy for this Example: Scanner sc = new Scanner(System.in); int age; do { System.out.println("How old are you?"); age = sc.nextInt(); } while (age < 0 || age > 100); System.out.println("You entered " + age);
3
Validation We could still easily make the program above break
enter “a” enter <esc>
4
Validating Scanner Input
So far, our programs have crashed if casts to numeric types failed: Try this with input “two point five”: import java.util.Scanner; public class ReadDouble2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); double stuff = 0.0; do { System.out.print("Input a double. Enter 0 to quit:"); stuff = input.nextDouble(); System.out.println("\nYou entered: " + stuff); } while (stuff != 0.0); }
5
Validating Scanner Input
Here is the simplest way to validate Scanner input for data format (that is, to make sure you get input that can be cast to double, integer, etc.) Scanner has hasNext…() methods that see if there is a parseable token hasNextInt() hasNextDouble() hasNextBoolean() Also need next() to skip over bad input
6
Validating Scanner Input
Try this one with input “nine.three”, then with input “nine point three: import java.util.Scanner; public class ReadDouble3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); Double inpDouble = 10.0; // bad code ahead!! do { System.out.print("Input a double. Enter 0 to quit:"); if(input.hasNextDouble()){ inpDouble = input.nextDouble(); System.out.println("\nYou entered: " + inpDouble); } else input.next(); } while (inpDouble != 0.0);
7
Validating Scanner Input
In order to get good output, we need to arrange things in a slightly more complex way: Scanner sc = new Scanner(System.in); String badInputString = null; System.out.println("Enter an integer"); while (!sc.hasNextInt()) { badInputString = sc.nextLine(); System.out.println(badInputString + " isn't an integer! Please try again."); } int x = sc.nextInt(); System.out.println(x);
8
Validating Both Format and Value
int value = 0; String badInputString = null; do { System.out.println("Enter an integer greater than 100"); while (!sc.hasNextInt()) { badInputString = sc.nextLine(); System.out.println(badInputString + " isn't an integer! Please try again."); } value = sc.nextInt(); sc.nextLine(); // throw away linefeed } while (value <= 100); System.out.println(value);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.