0
How to do a validation in java?
i want to do a validation on input like this: $500 - $600 So the input must contain $ and in a range of 500 till 600
1 Answer
+ 2
Scanner sc = new Scanner(System.in);
String str; int n=0;
System.out.println("enter $500 - $600:");
str=sc.nextLine().trim();
try { n = Integer.parseInt(str.substring(1) ); }
catch (NumberFormatException |
StringIndexOutOfBoundsException e) {} // ""
if (n>=500 && n<=600 && str.charAt(0)=='#x27;)
System.out.print("value "+str +" is OK");
else
System.out.print("incorrect value "+str);