Reputation: 23
I want to check whether the user entered double value is in the format XXX.YYY. If it is then it would return true otherwise false. I want to know if its possible to do this with decimalFormat class. People have said to use regex, but that is out of scope for me right now.
Upvotes: 0
Views: 195
Reputation: 1384
try this
String s = new String("123.456"); // suppose it is string from your file
String[] splitted = s.split("\\."); //take the part before and after do in the array , splitted is array of two strings
if(splitted.lenght == 2 && splitted[0].length() == 3 && splitted[1].length() == 3) // then it is in abc.xyz format
Upvotes: 0
Reputation: 3201
Pure java string solution:
public static boolean isValid(String str) {
if (str == null || str.length() != 7) {
return false;
}
return Character.isDigit(str.charAt(0)) && Character.isDigit(str.charAt(1)) && Character.isDigit(str.charAt(2))
&& str.charAt(3)=='.' &&
Character.isDigit(str.charAt(4)) && Character.isDigit(str.charAt(5)) && Character.isDigit(str.charAt(6));
}
public static void main(String[] args) {
System.out.println(isValid("123.456"));
System.out.println(isValid("12.456"));
System.out.println(isValid("abc.456"));
}
Upvotes: 1