take a look as the isDigit method in the Character class.
Character.isDigit(str.charAt(index))
if you do this in a loop and then break out when you hit a non-digit. You can then substring based on a count you incremented in the loop.
EDIT: I need glasses. No loops
Message was edited by:
_helloWorld_
> take a look as the isDigit method in the Character
> class.
>
> > Character.isDigit(str.charAt(index))
>
>
> if you do this in a loop and then break out when you
> hit a non-digit. You can then substring based on a
> count you incremented in the loop.
>
> EDIT: I need glasses. No loops
>
> Message was edited by:
> _helloWorld_
His homework assignment explicitly disallows looping
> With these conditionsit's easiest to use
> java.text.NumberFormat.import java.text.*;
> ...
> fmt=NumberFormat.getInstance();
> print(fmt.parse("1234hi")); // 1234
^^I never thought of that, I did think of this however.
public class Example {
static int index;
public static void main(String[] args) {
System.out.println(getNumbers("123xx"));
}
static String getNumbers(String s ) {
if(Character.isDigit(s.charAt(index))) {
index++;
getNumbers(s);
}
return s.substring(0, index);
}
}
Hi,
without iterator, i don't think it is possible.
try this one
String str="123hi";
String val="";
for(int i=0;i<str.length();i++)
{
if(Character.isDigit(str.charAt(i)))
val=val+str.charAt(i);
}
thn convert this string into integer.>