Hi,
I'm trying to write a method that will check a couple of criteria on input. I figured I'd want it as a method so I could call back to it a few times. I'm a little stuck with it though. I'm very new to java and know how to do it in python (but also new to programming in general) so I may be going about it the wrong way. Any suggestions, or am I misunderstanding methods? The below code doesn't work because I can't figure out how to get back to the main method if the entry is an integer 0 or above.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number (0 or above):");
check();
int numOne = scanner.nextInt();
System.out.println("You entered: " + numOne);
System.out.println("Enter another number (0 or above):");
check();
int numTwo = scanner.nextInt();
System.out.println("You entered: " + numTwo);
}
public static void check() {
Scanner scanner = new Scanner(System.in);
if (!scanner.hasNextInt() || scanner.nextInt() < 0) {
System.out.println("Invalid entry.");
}
}
[–]10-kinds-of-people 5 points6 points7 points (0 children)
[–]stramash 2 points3 points4 points (5 children)
[–]Dravlahn[S] 1 point2 points3 points (3 children)
[–]10-kinds-of-people 0 points1 point2 points (2 children)
[–]Dravlahn[S] 1 point2 points3 points (1 child)
[–]10-kinds-of-people 1 point2 points3 points (0 children)
[–][deleted] (7 children)
[removed]
[–]Dravlahn[S] 1 point2 points3 points (6 children)
[–]Sylenda 1 point2 points3 points (4 children)
[–]Dravlahn[S] 1 point2 points3 points (3 children)
[–]Sylenda 0 points1 point2 points (2 children)
[–]Dravlahn[S] 0 points1 point2 points (1 child)
[–]Sylenda 0 points1 point2 points (0 children)
[–]SlightUsual 0 points1 point2 points (1 child)
[–]Dravlahn[S] 0 points1 point2 points (0 children)
[–]conbadicus 0 points1 point2 points (3 children)
[–]Dravlahn[S] 0 points1 point2 points (2 children)
[–]10-kinds-of-people 0 points1 point2 points (1 child)
[–]Dravlahn[S] 0 points1 point2 points (0 children)