   string manipulations
  [Java - compiling with javac]In my JApplet, a user can enter in text at a JTextField. Now, due to the nature of my program, some of this input may be jibberish, and some of it may be "valid."By valid, I mean that I am attempting to filter a number (whole or decimal) out of the string. Just one number, and only the first one it extracts. For instance, given the following input: "djijri3.5839djrhj", I would want my routine to extract "3.5839" so that I could save it into a double-type variable. Or given the input, "wkjdi638dsjdij93284sk", I would want to extract the "638" and save it into an int-type variable.Here's the source code for what I have so far. It compiles, but throws over 20 exceptions at runtime, including null pointer exceptions, and the likes:String extractNumber(String str){    if(str == "" || str == null) return null;    int i;    String temp = "";    for(i = 0; i < str.length(); i++)    {   // If the current character is between 0 and 9, or if it's a// decimal point, it's a number.if((str.charAt(i) >= '0' && str.charAt(i) <= '9') ||   str.charAt(i) == '.')   {  // While the string character range between 0 and 9 or '.'  // (decimal), AND while the iterator is less than the passed  // string's length, continue appending to the temp string.    while((str.charAt(i) >= '0' && str.charAt(i) <= '9') ||   str.charAt(i) == '.') && i < str.length())    { temp += str.charAt(i);i++;    }   }    }    return temp;}The code is pretty straight-forward: if the current character is between '0' and '9', or if it is a decimal point, then it must be part of a number (don't worry about error checking!!!). It then slips into a while loop that continues appending the current character to the string so long as the above conditions are true and also so long as the iterator does not reach the end of the string.What's going on with all these exceptions? Although there are too many for me to list here, the first one (and probably the most important) is:Exception thrown in thread: AWT-Queue-1 StringIndexOutofBoundsExceptionString index is out of range...   
  while((str.charAt(i) >= '0' && str.charAt(i) <= '9') || str.charAt(i) == '.') && i < str.length())The parentheses here don't balance - I count 6 opens and 7 closes. Is that a typo you introduced when you pasted/copied? I suspect your problem is around there.Anyhow, you are monkeying with your i inside the loop and that's always a bit squirrley. I don't much believe in programming taboos (if that even is one) but I think there are better ways to set that loop up that would be easier to read/debug/etc.Since all you want is the first set of character that can be interpreted as a number, you can just hop out of the loop when a non-numeric character is encountered && temp.length > 0 or when you run out of str to check.   ;  hai,concerning your inner loop:    while((str.charAt(i) >= '0' && str.charAt(i) = '0'will throw an exception. I guess your loop should be something like    while(i = '0' && str.charAt(i) <   ;  Your error is due to the fact that in your inner loop, you are incrementing the iterator i without checking if it exceeds str.length().   ;  hmmm... sorry, my prevouous above seems to bee scrambled up somehow... not what i wanted. Does anyone else have this problem or is it just me? I'm new to posting on gamedev. :Danyway, my first remark was about unbalanced parantheses aswell.Secondly, i think the inner while loop throws an exception at the first test, since you test str.charAt(i) before you test i to be within range.I guess your loop should be something likewhile(i  = '0' && str.charAt(i) <   ;  ok, i give up. don't seem to be able to put code here without it being interpertated as html tags.... :(. but the others are right, start with while (i < str.charAt(i) && // ... continue other testsput immediatly after your while loop something likei = str.length()to break the outer for loop. this way you'll only get the first number.;  Hi guys - thank you for the good suggestions, but I'm still a little confused here.Everyone indicated that my inner while() loop is throwing the exceptions, and now (thanks to you all) I understand that it's because the iterator is exceeding the length of the str string variable.However, I do not understand at all the code many of you recommended, which looks like this:while(i = '0' && str.charAt(i) <Now I know the 'Anonymous Poster' who made three attempts to post the rest of that code was having trouble making his posts, and see he/she could not finish the rest of the suggested while statement. However I don't even understand the purpose of that assignment statement in the beginning of the loop....If 'i' is my iterator - whose purpose is to scroll through the 'str' string, why would I assign it a value? Furthermore, as an int-type variable, how can I assign it to a character???Could anybody post a leedle bit of source to help me out and patch this function up?   ;  You don't need the assignment.  You just need to change the order of your expressions in the while() statement and remove one close paranthesis.  I'm not real familiar with Java, but I'm surprised that even compiles.  dalep is right that you have more close paranthesis than open.  Also, you probably want to keep track of how many decimal points you have.  If you come to a second one it should stop the number right there (for all I know, Java might just ignore extra decimal points, but you should still do it for performance and clarity's sake).You have this:    while((str.charAt(i) >= '0' && str.charAt(i) <= '9') ||   str.charAt(i) == '.') && i < str.length())You should have this:    while(i < str.length() && (str.charAt(i) >= '0' && str.charAt(i) <= '9') || str.charAt(i) == '.');  Sorry, I made a mistake on that last post and I can't edit it because I'm anonymous:    while(i < str.length() && ((str.charAt(i) >= '0' && str.charAt(i) <= '9') || str.charAt(i) == '.'))