On Mar 23, 10:14 am, "Andrew Thompson" <andrewtho...@gmail.com> wrote:
On Mar 24, 12:54 am, "Lara" <the_brat_...@yahoo.com> wrote:
...
String firstnum =JOptionPane.showInputDialog("Enter first number");
How can I check if (firstnum) is number or not befor converting the
String to Integer??
You do not have to. You might try it,
and catch the result of failure. If it
fails, it means the string does not
represent an integer.
Andrew T.
private static Pattern p = Pattern.compile("\\d+");
public static void main(String[] args) {
String s = "3456";
Matcher m = p.matcher(s);
int number = 0;
if (m.matches()) {
number = Integer.parseInt(s);
System.out.println("The number entered is = " + number);
} else {
System.out.println("Not a number = " + s);
}
int number2 = 0;
String s2 = "x3456";
m = p.matcher(s2);
if (m.matches()) {
number = Integer.parseInt(s);
System.out.println("The numner entered is = " + number2);
} else {
System.out.println("Not a number = " + s2);
}
}