3

we have this method

 System.out.print("Enter Principal Amount (1k to 1m) ") ;
        while (true) {
            principal = scanner.nextInt();
            if (principal >= 1000 && principal <= 1_000_000)
                break;
            System.out.println("Enter a value between 1k - 1m");

If person puts in letters instead of numbers the error occurs, I need to get the program to ask the question to put in numbers instead of letter

 Scanner scanner = new Scanner(System.in);

        System.out.print("Enter Principal Amount (1k to 1m) ") ;
        while (true) {
            principal = scanner.nextInt();
           // if (scanner.nextInt() != NumberFormat??) What should I write in my mortgage calculator that ->
            //  --> if numbers are not put (for ex. letters) it would print out ("Please enter numbers letters are invalid")
            //System.out.println("Please enter numbers");
            if (principal >= 1000 && principal <= 1_000_000)
                break;
            System.out.println("Enter a value between 1k - 1m");
        }
sartep
  • 69
  • 4
  • So do you want the user to be able to enter "1k" and have that translate to the int value 1000 internally? – OH GOD SPIDERS Feb 10 '23 at 14:26
  • If what @OHGODSPIDERS said is true, just check the string for k's and replace them. A pretty easy solution, but it can also be exploited as easily. To improve on this you could set the max k-count to 1 and always check for it to be at the last position in the string. – Z-100 Feb 10 '23 at 14:30
  • No, if for ex. user enters "abc", I get = Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:943) at java.base/java.util.Scanner.next(Scanner.java:1598) at java.base/java.util.Scanner.nextInt(Scanner.java:2263) at java.base/java.util.Scanner.nextInt(Scanner.java:2217) at com.petras.Main.main(Main.java:20) But I want to get the line "Please enter valid number" //not letter and let him put the number again, how should my code look? – sartep Feb 10 '23 at 14:34

2 Answers2

1

The simplest way to do what you want is to wrap the inside of your loop in a try {} block and catch the InputMismatchException

If the input is an int it will process as it does now; if not then instead of carrying on after the Scanner.nextInt() line, java will execute the contents of your catch(InputMismatchException e) { } block

There's an argument that you should validate the input more explicitly, but that will be more complex and for a beginner learning how to catch exceptions is probably more useful.

while (true) {
    try {
        principal = scanner.nextInt();
        if (principal >= 1000 && principal <= 1_000_000)
            break;
        System.out.println("Enter a value between 1k - 1m");
    }
    catch (InputMismatchException e) {
        System.out.println("Please enter numbers");
    }
}
Andrew McGuinness
  • 2,092
  • 13
  • 18
0

From the comments, the OP replied:

No, if for ex. user enters "abc", I get = Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:943) at java.base/java.util.Scanner.next(Scanner.java:1598) at java.base/java.util.Scanner.nextInt(Scanner.java:2263) at java.base/java.util.Scanner.nextInt(Scanner.java:2217) at com.petras.Main.main(Main.java:20) But I want to get the line "Please enter valid number" //not letter and let him put the number again, how should my code look?

The problem is that your code assumes the user will always enter a valid integer value. Instead, as your comment reply indicates, you have to account for invalid inputs by wrapping scanner.nextInt() in a try/catch block

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Principal Amount (1k to 1m) ") ;
int principal = 0;
while (true) {
  try {
    principal = scanner.nextInt();
  } catch(InputMismatchException ime) {
    System.out.println("Invalid input: " + principal + ". Please enter an integer value");
    continue;
  } catch (Exception e) {
    // This is most likely a "true" error. Handle accordingly.
  }
  if (principal >= 1000 && principal <= 1_000_000)
    break;
  System.out.println("Enter a value between 1k - 1m");
}
hfontanez
  • 5,774
  • 2
  • 25
  • 37