this is my code :
String addchar = "";
String tempchar;
int len = strline.length();
char[] chars = strline.toCharArray();
int amount = 0;
for (int i = 0; i < len; i++) {
tempchar = String.valueOf(chars[i]);
if (tempchar == "0" || tempchar == "1" || tempchar == "2" || tempchar == "3" || tempchar == "4" || tempchar == "5" || tempchar == "6" || tempchar == "7" || tempchar == "8" || tempchar == "9") {
addchar=tempchar+addchar;
}
}
amount=Integer.parseInt(addchar);
but when run this code , I see that amount
is empty!
I want to extract , the number that there is in strline
Slightly less elegant than Nilesh's answer, but as an alternative:
StringBuilder sb = new StringBuilder();
for (char c : strline.toCharArray()) {
if ((sb.length() == 0 && "-".equals(c)) || Character.isDigit(c)) sb.append(c);
}
int amount = Integer.parseInt(sb.toString());
Edit Amended to allow for initial negative sign