Reputation: 11
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)
Upvotes: 0
Views: 5277
Reputation: 12939
change
a = Integer.parseInt(num1);
to
try {
a = Integer.parseInt(num1);
} catch(NumberFormatException ex) {
System.out.println("Error, "+num1+" isn't a whole number!");
}
Upvotes: 0
Reputation: 441
Try using a method to convert your characters into their ASCII representations.
Upvotes: 0
Reputation: 22233
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
Upvotes: 2
Reputation: 9437
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.
Upvotes: 0
Reputation: 27356
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.
Upvotes: 0
Reputation: 122008
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"
Upvotes: 0
Reputation: 4361
a = Integer.parseInt(num1);
Your are parsing num1 which is String to Integer. This causes exception
Upvotes: 1