How to do a validation in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

20th Jun 2019, 6:00 PM
REXH
REXH - avatar
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);
20th Jun 2019, 8:14 PM
zemiak