Код: Выделить всё
private static void addPetrol(Vehicle v){
Scanner scanner = new Scanner(System.in);
System.out.print("How many gallons of Petrol would you like to add: ");
while (true) {
String input = scanner.next();
System.out.println();
try {
float gallons = Float.parseFloat(input);
v.addPetrol(gallons);
System.out.println("Adding " + gallons + " gallons of fuel to the tank");
break;
}
catch (VechicleFuelException e) {
System.out.println(e.getMessage() + " Try again: ");
}
catch(Exception e){
System.out.println("Input " + input + " is not a valid amount, try again: ");
}
// Consume the leftover newline if there is any
if (scanner.hasNextLine()) {
scanner.nextLine();
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... g-error-or
Мобильная версия