2

I have a console application where I need to input numbers until enter "x". Of course when I input "x" i will get a NumberFormatException.

How would I quit the program when entering "x" without getting an exception.

BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
    String s;
    int input;
    String name = args[0];
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    Date date = new Date();


    System.out.println("Good morning " + name + " Today's date is " + sdf.format(date));

    System.out.println("Please enter any number between 0 and 10");

    try
    {


        do
        {
        s = buf.readLine();

        input = Integer.parseInt(s);

        while(input <= 0 || input > 10)
        {
            System.out.println("Make sure about the correct input...between 0 and 10 please");
            s = buf.readLine();
            input = Integer.parseInt(s);
            System.out.println(input);
        }
        }while(s != "x");

7 Answers 7

3

Just add a line

s = buf.readLine();
if ("x".equals(s)) break; // add this line

This does not guarantee that s is an integer, so you still need to catch an exception like andreas proposed

Sign up to request clarification or add additional context in comments.

Comments

3

Reorder the loop(s) to check if s is equal to x before Integer.parseInt(). Use String.equals() to compare strings, not == or !=.

As this is homework I will not post modified code.

EDIT:

Just to explain the reason for using String.equals():

From section 15.21.3 Reference Equality Operators == and != of the Java Language Specification 3.0:

While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters. The contents of two strings s and t can be tested for equality by the method invocation s.equals(t).

Comments

2

Wrap the code in try..catch block.

input=-1;
try{
  input = Integer.parseInt(s);
  while(input <= 0 || input > 10)
  {
   System.out.println("Make sure about the correct input...between 0 and 10 please");
   s = buf.readLine();
   input = Integer.parseInt(s);
   System.out.println(input);
   }
}catch(Exception ex) { }
....

Comments

2

Wrap the Integer.parseInt statement in a try/catch block:

try {
 input = Integer.parseInt(s);
} catch{NumberFormatException nfe) {
 System.out.println("Illegal input");
 // know you could do one of the following: (uncomment)
 // continue;        // would continue the while loop
 // break;           // would exit the while loop
 // System.exit(0);  // would exit the application
}

Comments

2

Change to a while loop and do

while (s != "x") {
  // your logic

}

or check if s != "x" before this line:

input = Integer.parseInt(s);

Comments

2

Catch the exception:

s = buf.readLine();
if("x".compareToIgnoreCase(s)) {
  // im quit
  return;
}

Comments

1

Add a if statement before paring the string to a int:

    if(s.equals("x"))
       system.exit(1);

    else{
    input = Integer.parseInt(s);

            while(input <= 0 || input > 10)
            {
                System.out.println("Make sure about the correct input...between 0 and 10 please");
                s = buf.readLine();
                input = Integer.parseInt(s);
                System.out.println(input);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.