Search code examples
javabluej

How do I fix an if statement that uses "parseInt()" so that the else portion is activated and my program doesn't fail?


I wrote a program that asks for user input (numbers), then tells the user which of the two numbers they put in are bigger after making sure both values are numbers. This is looped. The problem is, when the user enters something that isn't valid, the program shuts down instead of referring to my else statements. I think this is because text values in strings cannot be processed using parseInt() so instead of the program realizing that a text value isn't a valid number, it just fails. I am using BlueJ to make this program if that helps anyone solve the problem. I am also open to people telling me how to make my program more efficient/easier to code (I am a beginner).

import java.util.Scanner;
public class TemperatureDriver {
    public static void main(String[] args) {
        while (true) {
            Scanner keyInput = new Scanner(System.in); 
            System.out.print("Enter a number"+ "\n");
            String number_one = keyInput.next();

            if (Integer.parseInt(number_one) <=0 || Integer.parseInt(number_one) > 0) {
                System.out.print("Enter another number" + "\n");
                String number_two = keyInput.next();     

                if(Integer.parseInt(number_two) <=0 || Integer.parseInt(number_two) > 0){

                    if (Integer.parseInt(number_one) > Integer.parseInt(number_two)) {
                        System.out.print(number_one + " is greater than " + number_two + "\n" + "\n");
                    } else if(Integer.parseInt(number_one) < Integer.parseInt(number_two)){
                        System.out.print(number_one + " is less than " + number_two + "\n" + "\n");
                    } else if(Integer.parseInt(number_one) == Integer.parseInt(number_two)){
                        System.out.print(number_one + " is equal to " + number_two + "\n" + "\n");
                    }                

                } else {
                    System.out.println("Invalid number!"+ "\n");
                }  

            } else {
                System.out.println("Invalid number!"+ "\n");
            }  

        }
    }
}

Solution

  • Your code where you call parseInt should be embedded in a try block, which should be followed by a catch block to catch the NumberFormatException.

    try {
        if (Integer.parseInt(number_one) <= 0 || Integer.parseInt(number_one) > 0) {
          System.out.print("Enter another number" + "\n");
          String number_two = keyInput.next();
    
          if (Integer.parseInt(number_two) <= 0 || Integer.parseInt(number_two) > 0) {
    
            if (Integer.parseInt(number_one) > Integer.parseInt(number_two)) {
              System.out.print(number_one + " is greater than " + number_two + "\n" + "\n");
            } else if (Integer.parseInt(number_one) < Integer.parseInt(number_two)) {
              System.out.print(number_one + " is less than " + number_two + "\n" + "\n");
            } else if (Integer.parseInt(number_one) == Integer.parseInt(number_two)) {
              System.out.print(number_one + " is equal to " + number_two + "\n" + "\n");
            }
    
          } else {
            System.out.println("Invalid number!" + "\n");
          }
    
        } else {
          System.out.println("Invalid number!" + "\n");
        }
      } catch (NumberFormatException e) {
        System.out.println("Not a valid number");
      }