-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dowhile.java
30 lines (24 loc) · 912 Bytes
/
Dowhile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.InputMismatchException;
import java.util.Scanner;
public class Dowhile {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int value;
do {
System.out.print("Please enter a number: ");
try {
value = scanner.nextInt();
} catch (InputMismatchException e) {
// Handle the case where the input is not an integer
System.out.println("Error: Invalid input. Please enter an integer.");
// Clear the scanner's buffer
scanner.nextLine();
// Set value to continue loop
value = Integer.MIN_VALUE;
}
} while (value != 5);
System.out.println("Got 5!");
// Close the scanner to prevent resource leak
scanner.close();
}
}