How to Get a Number From a String of Unknown Size (Java)
<p>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 <em>get the exact number from the string</em>. Here are the two methods I am working with:</p>

<pre><code>//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 &lt; 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;
}
</code></pre>

<p>and:</p>

<pre><code>//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&lt;Char&gt; digits = new List&lt;Char&gt;();
    //a char array so I can loop through the string
    char[] c = s.toCharArray();

    for(int i = 0; i &lt; 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.
}
</code></pre>

<p>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?</p>

<p>EDIT: Thanks guys, I looked into regex, but I don't think that'll do the job. I am looking for <em>one</em> 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.</p>