Zdążyłem już to ogarnąć, choć zrobiłem to nieco inaczej.
public static boolean isInteger(String s){
try{
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
} catch(NullPointerException e) {
return false;
}
return true;
}
public static void podajLiczbe(){
System.out.print("Podaj liczbę: ");
String n_str;
int n_int;
Scanner in = new Scanner(System.in);
n_str = in.nextLine();
if(isInteger(n_str) == false){
while(isInteger(n_str) != true){
System.out.println("Podana przez Ciebie łańcuch nie jest liczbą. Podałeś: " + n_str);
System.out.print("Podaj liczbę: ");
n_str = in.nextLine();
if(isInteger(n_str) == true){
n_int = Integer.parseInt(n_str);
System.out.println("Podałeś: " + n_int);
}
}
}
else{
n_int = Integer.parseInt(n_str);
System.out.println("Podana liczba to: " + n_int);
}
}
Swoją drogą nie rozumiem skąd tyle minusów, bo ludzie potrafią powielać temat typu "java czy c++" itp podobne bzdury i wszystko przebiega bez minusów, a ja po prostu chciałem poznać metodę z rzutowaniem (o ile taka istnieje), a nie samo wykorzystanie obsługi wyjątków, no ale okej. Niby w niczym nie szkodzą mi te minusy, ale zastanawiam się skąd takie oburzenie. :D