I have just went through a 70 video series on java. Time to take off training wheels so I am making a very simple program to take 2 text inputs for a class. It works the first making of a Car
but throws an error on the second. I can not figure out why.
import java.util.Scanner;
class Car{
public int cost;
public double mpg;
public void enterInfo() {
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter cost: ");
this.cost = reader.nextInt(); // Scans the next token of the input as an int.
System.out.println("Enter MPG: ");
this.mpg = reader.nextDouble();
reader.close();
}
}
public class App {
public static void main(String[] args) {
Car car1 = new Car();
car1.enterInfo();
Car car2 = new Car();
car2.enterInfo();
System.out.println(car1.cost);
}
}
Here's the error
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at uda01.Car.enterInfo(App.java:13)
at uda01.App.main(App.java:32)