0

This program will solve the numbers that you will input but I want it to show ERROR if I will input some letters. Thanks for helping

This is my code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class jedz
{
    public static void main (String args[])
    {
       BufferedReader dataln = new BufferedReader(new InputStreamReader(System.in));
       int a, b, c;
       String num1 =" ";
       String num2 =" ";
       System.out.print("Enter FN: ");
       try {
          num1 = dataln.readLine();
       }
       catch (IOException e) 
       {
          System.out.println("Error");
       }
       a = Integer.parseInt(num1);
       System.out.print("Enter SN: ");
       try
       {
           num2 = dataln.readLine();
       }
       catch (IOException e) 
       {
           System.out.println("Error");    
       }
       b = Integer.parseInt(num2);
       c = a + b;
       System.out.print("The answer is "+ c) ;
    }
}

This is the output when I put some letters.

D:\>java jedz
Enter FN: s
    Exception in thread "main" java.lang.NumberFormatException: For input string: "s "
        at java.lang.NumberFormatException.forInputString(NumberFormatException. java:65)
        at java.lang.Integer.parseInt(Integer.java:492)
        at java.lang.Integer.parseInt(Integer.java:527)
        at jedz.main(jedz.java:21)
user3817065
  • 11
  • 1
  • 2
  • 4
  • Once you have read num1, check it is a number (using regex for example), if it is not show your error message and ask the user to re-enter it (you can use a while loop) – StephaneM Jul 09 '14 at 12:54

7 Answers7

2

I want it to show ERROR if I will input some letters

You can use the Scanner class to check if there is an int available:

Scanner dataln = new Scanner(System.in);

if(dataln.hasNextInt()) {
    a = dataln.nextInt();
} else {
    //no ints, display error
}

Otherwise you can use your current solution and add a try-catch block

try{
    a = Integer.parseInt(num1);
}
catch (NumberFormatException e) 
{
    System.out.println("Error");
}

The first solution gives you better performance. See this question

Community
  • 1
  • 1
BackSlash
  • 21,927
  • 22
  • 96
  • 136
1
a = Integer.parseInt(num1);

Your are parsing num1 which is String to Integer. This causes exception

Pracede
  • 4,226
  • 16
  • 65
  • 110
  • no, it doesn't. "5" is also a String, but parsing it won't cause an Exception to be thrown. The Exception is thrown because the value is not a valid numerical value. – Stultuske Jul 09 '14 at 12:57
  • This is what i see in the stacktrace Enter FN: s – Pracede Jul 09 '14 at 13:05
  • indeed, but it's not because num1 is a String. as I said, "5" is also a String. it's because the value of num1 is not a valid numeric value, not because it is a String. – Stultuske Jul 09 '14 at 13:06
  • ok i said it in other words. You entered "s" it cannot parse it into Integer. – Pracede Jul 09 '14 at 13:10
  • you didn't say it in other words, what you said was correct, just not entirely complete. if the String was a valid numerical value, it wouldn't throw an Exception, so in those cases the part "String to Integer. This causes exception" would not be right. it also doesn't parse it to an Integer, but to an int. – Stultuske Jul 09 '14 at 13:17
  • Yes i see what you want to say. Yes you're right it is not because it is String but because it is "s" – Pracede Jul 09 '14 at 13:21
0

cause:

a = Integer.parseInt(num1);

The above line executes successfully then and then only when user enters a correct integer string.

That obvious that the string "s" is not a valid integer.

solution:

One thing you can do wrap your parseInt code in try catch block and prompt user in catch block with message "invalid input"

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

No effort? Then tutorials for you!

Firstly, you need to learn about basic validation in Java. In short, you're trying to put some letters into a denary number, which it isn't going to like. You need to read a bit about Regex matching. You can use regular expressions to test the content of String variables.

Secondly, look into Exception Handling. This allows you to catch an error that is thrown, and output a meaningful message to the user.

Your Problem...

a = Integer.parseInt(num1);

Will throw an exception because you're not checking the value.

christopher
  • 26,815
  • 5
  • 55
  • 89
0

Well ... "s" is a valid String, but it is not a numerical value, which is why you can not cast it to a number.

So, if you run this:

a = Integer.parseInt(num1);

And the value of num1 = "5", then there's no problem, because 5 is a numerical value within the boundaries of an int. An "s" however, will throw the exception you get.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

Try using a method to convert your characters into their ASCII representations.

JBires
  • 441
  • 3
  • 11
0

change

a = Integer.parseInt(num1);

to

try {
  a = Integer.parseInt(num1);
} catch(NumberFormatException ex) {
  System.out.println("Error, "+num1+" isn't a whole number!");
}
kajacx
  • 12,361
  • 5
  • 43
  • 70