I am making a part of a program that will check if a string entered into a JTextArea is a number, and if so, what number the string contains (by the way, not the whole string contains a number, and I don't know how many digits the number is). I already know how to get the string from the JTextArea and how to check if the string contains a number. But I don't know how to get the exact number from the string. Here are the two methods I am working with:
//no problems with this method, it's just here for reference.
public static boolean isNum(char[] c, int index){
//I want to include numbers 0-9
for(int i = 0; i < 10; i++){
if(c[index].equals(i(char)) || c[index].equals('.')){
return true;
}
}
//if the character is not a number 0-9, it is not a number, thus returning false.
return false;
}
and:
//I need a string parameter to make it easier to get the text from the JTextArea
public static float checkNum(String s){
//a List to hold the digits
List<Char> digits = new List<Char>();
//a char array so I can loop through the string
char[] c = s.toCharArray();
for(int i = 0; i < c.length(); i++){
//if the character is not a number, break the loop
if(!isNum(c[i])){
break;
}
else{
//if the character is a number, add it to the next digit
digits.add(c[i]);
}
}
//insert code here.
}
Maybe I should convert the char List back into a char array, then convert it to a string, then convert it to a float? If so, how would I do that?
EDIT: Thanks guys, I looked into regex, but I don't think that'll do the job. I am looking for one number with an unknown number of digits. I do know that the number will have a space at the end (or at least a non-numeric value), though.