简体   繁体   中英

How to Get a Number From a String of Unknown Size (Java)

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.

You should use a regular expression. In java you could loop through every instance of digits like this:

java.util.regex.Pattern;
java.util.regex.Matcher;

Pattern p = Pattern.compile("\\d+?\\.\\d+");
Matcher m = p.matcher(inputString);

while(m.find())
  //do some string stuff

Or you could look for one match within a string with one group of digits by replacing the while loop with this:

String digits = m.group(1);
double number = Double.valueOf(digits);

For more information on how this works look up regular expressions. This site is particularly helpful https://regexone.com/

You can use a regular expression to test and extract a number of any length. Here is an example simple method that that does just that:

public Integer extractNumber(String fromString){
    Matcher matcher = Pattern.compile("\\D*(\\d+)\\D*").matcher(fromString);
    return (matcher.matches()) ? new Integer(matcher.group(1)) : null;
}

If you want to handle decimals within the number, you can change the method to:

public Double extractNumber(String fromString){
    Matcher matcher = Pattern.compile("\\D*(\\d+\\.?\\d+)\\D*").matcher(fromString);
    return (matcher.matches()) ? new Double(matcher.group(1)) : null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM