Arianule
Arianule

Reputation: 9043

Console input java. Parsing a string

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");

Upvotes: 2

Views: 1676

Answers (7)

stefan bachert
stefan bachert

Reputation: 9608

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

Upvotes: 3

hmjd
hmjd

Reputation: 121971

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).

Upvotes: 3

Pau Kiat Wee
Pau Kiat Wee

Reputation: 9505

Catch the exception:

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

Upvotes: 2

Darren
Darren

Reputation: 70728

Change to a while loop and do

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

}

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

input = Integer.parseInt(s);

Upvotes: 2

Andreas Dolk
Andreas Dolk

Reputation: 114787

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
}

Upvotes: 2

KV Prajapati
KV Prajapati

Reputation: 94645

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) { }
....

Upvotes: 2

John Snow
John Snow

Reputation: 5334

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);
}

Upvotes: 1

Related Questions