Most likely first next()
picks up dsa
, then nextInt()
picks up 435 and second next()
pick up the carriage return. So the second nextInt()
picks up salon
, causing your mismatch exception. So like I said, just read line by line, split the line and parse the int.
EDIT:
for two words or more you need to get the index of the integer and get the substring
public class Test {
public static void main(String[] args) {
String line = "Trump Tower 23445";
int pos = line.replaceFirst("^(\\D+).*$", "$1").length();
String number = line.substring(pos);
System.out.println(Integer.parseInt(number));
System.out.println(line.substring(0, pos));
}
}
Result
23445
Trump Tower
If the regex is too hard for you to understand, you could always just create a method to get the index of the first int
public static int getIndexOfFirstInt(String line) {
char[] chars = line.toCharArray();
for (char c : chars) {
if (Character.isDigit(c)) {
return line.indexOf(c);
}
}
return -1;
}