[Solved] i need help resolving this java code [closed]


This just reads and tries to parse the number, if it fails, then it just move on to the while loop’s guard.

Also you should try to use Integer.MAX_VALUE instead of a random number. Just in case someone decides to actually use the max value; don’t assume that 9,999,999 or how ever many 9’s you want is the max as

  1. You will actually surpass the max value and cause an error
  2. Not actually reach the max value
  3. Magic Numbers are bad

For more info on max value: https://en.wikipedia.org/wiki/2147483647_(number)

Scanner keyboard = new Scanner(System.in);

int smallest = Integer.MAX_VALUE;
String input = "";

System.out.println("This program find the smallest number" 
    + " in a series of numbers");
System.out.println("When you want to exit, type Q");

while (!input.toLowerCase().equals("q")) {
    System.out.print("Enter a number: ");
    input = keyboard.next();
    try {
        int numb = Integer.parseInt(input);
        if (numb < smallest) 
            smallest = numb;
    } catch (NumberFormatException e) {
        // Maybe check for other random strings here?
        // If you expect only "Q" or a number, then no need
    }
}

System.out.println("Smallest number: " + smallest);
System.exit(0);

0

solved i need help resolving this java code [closed]