英文:
Exception thrown while calling scanner more than once. "Exception in thread main" java.util.NoSuchElementException: No line found"
问题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Would you like to test a used car today? : y or n");
if ("y".equals(scanner.nextLine())) {
while (true) {
Car testCar = new Car();
testCar.drivingTest();
testCar.carAgeType();
break;
}
} else {
System.out.println("dang");
}
scanner.close();
}
}
public class Vehicle {
private String licence, MOT;
public Vehicle() {
this("null", "null");
}
public Vehicle(String licence, String MOT) {
this.licence = licence;
this.MOT = MOT;
}
public String getLicence() {
return licence;
}
public String getMOT() {
return MOT;
}
public void drivingTest() {
Scanner DTScan = new Scanner(System.in);
System.out.println("great , do you have a driving licence :type: big, medium or small");
String reply = DTScan.nextLine();
if ("big".equals(reply) || "medium".equals(reply) || "small".equals(reply)) {
this.licence = reply;
} else {
this.licence = "fail";
}
System.out.println("Great you passed your '" + licence + "' exam. Lets check the MOT : type: pass or fail");
reply = DTScan.nextLine();
if ("pass".equals(reply) || "fail".equals(reply)) {
this.MOT = reply;
} else {
this.MOT = "invalid";
}
DTScan.close();
}
}
public class Car extends Vehicle {
private String type;
private int age;
public Car() {
}
public Car(String licence, String MOT, String type, int age) {
super(licence, MOT);
this.type = type;
this.age = age;
}
public String getType() {
return type;
}
public int getAge() {
return age;
}
public void carAgeType() {
Scanner ATScan = new Scanner(System.in);
System.out.println("What type of car were you looking for :type: sports , town or 4X4");
ATScan.nextLine();
String reply = ATScan.nextLine();
if ("sports".equals(reply) || "town".equals(reply) || "4X4".equals(reply)) {
this.type = reply;
} else {
this.type = "invalid";
}
System.out.println("And what sort of Age :type: between 1 and 10");
int years = ATScan.nextInt();
if (years > 0 && years < 10) {
this.age = years;
} else {
this.age = -1;
}
ATScan.close();
}
}
英文:
I am pretty new at Java and am just learning so please be kind.
I am doing a coding Inheritance challenge and I'm trying to use user inputs to set up a car before driving.
When I call the 2 methods I am using to set the parameters with Scanner this first method testCar.drivingTest()
works fine, however when testCar.carAgeType()
is called, I get an exception.
Below are the requests and inputs as well as the exception.
Would you like to test a used car today? : y or n
y
great , do you have a driving licence :type: big, medium or small
big
Great you passed your 'big' exam. Lets check the MOT : type: pass or fail
pass
What type of car were you looking for :type: sports , town or 4X4
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at learning.java.Car.carAgeType(Car.java:30)
at learning.java.Main.main(Main.java:17)
Process finished with exit code 1
From what I can gather searching google most people use .hasNextLine() to check something but I don't know what to do or why this is coming back as false on testCar.carAgeType. both methods use basically the same code and when I call only one of the methods testCar.carAgeType or testCar.drivingTest they work fine when on there own but when called one after the other I get the exception. I am also unsure what to do when I use .hasNextLine and it comes back false.
Can anyone help me understand why Scanner throws an exception when it is called in multiple methods?
All help is much appreciated, thanks.
Code sample bellow has main and 2 classes, Vehicle parent and car child
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.println("Would you like to test a used car today? : y or n");
if ("y".equals(scanner.nextLine())){
while (true) {
Car testCar = new Car();
testCar.drivingTest();
testCar.carAgeType();
break;
}
}else{
System.out.println("dang");
}
scanner.close();
}
}
import java.util.Scanner;
public class Vehicle {
private String licence, MOT;
public Vehicle(){
this("null", "null");
}
public Vehicle(String licence, String MOT) {
this.licence = licence;
this.MOT = MOT;
}
public String getLicence() {
return licence;
}
public String getMOT() {
return MOT;
}
public void drivingTest(){
Scanner DTScan = new Scanner (System.in);
System.out.println("great , do you have a driving licence :type: big, medium or small");
String reply = DTScan.nextLine();
if ("big".equals(reply) || "medium".equals(reply) || "small".equals(reply)) {
this.licence = reply;
} else {
this.licence = "fail";
}
System.out.println("Great you passed your '" + licence + "' exam. Lets check the MOT : type: pass or fail");
reply = DTScan.nextLine();
if ("pass".equals(reply) || "fail".equals(reply)) {
this.MOT = reply;
} else {
this.MOT = "invalid";
}
DTScan.close();
}
}
import java.util.Scanner;
public class Car extends Vehicle{
private String type;
private int age;
public Car(){
}
public Car(String licence, String MOT, String type, int age) {
super(licence, MOT);
this.type = type;
this.age = age;
}
public String getType() {
return type;
}
public int getAge() {
return age;
}
public void carAgeType(){
Scanner ATScan = new Scanner (System.in);
System.out.println("What type of car were you looking for :type: sports , town or 4X4");
ATScan.nextLine();
String reply = ATScan.nextLine();
if ("sports".equals(reply) || "town".equals(reply) || "4X4".equals(reply)) {
this.type = reply;
} else {
this.type = "invalid";
}
System.out.println("And what sort of Age :type: between 1 and 10");
int years = ATScan.nextInt();
if (years > 0 && years < 10) {
this.age = years;
} else {
this.age = -1;
}
ATScan.close();
}
}
答案1
得分: 1
以下是翻译好的代码部分:
class Main34 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Would you like to test a used car today? : y or n");
String val = scanner.nextLine();
if ("y".equals(val)) {
while (true) {
Car testCar = new Car();
testCar.drivingTest(scanner);
testCar.carAgeType(scanner);
break;
}
} else {
System.out.println("dang");
}
scanner.close();
}
}
class Vehicle {
private String licence, MOT;
public Vehicle() {
this("null", "null");
}
public Vehicle(String licence, String MOT) {
this.licence = licence;
this.MOT = MOT;
}
public String getLicence() {
return licence;
}
public String getMOT() {
return MOT;
}
public void drivingTest(Scanner scanner) {
System.out.println("great , do you have a driving licence :type: big, medium or small");
String reply = scanner.nextLine();
if ("big".equals(reply) || "medium".equals(reply) || "small".equals(reply)) {
this.licence = reply;
} else {
this.licence = "fail";
}
System.out.println("Great you passed your '" + licence + "' exam. Lets check the MOT : type: pass or fail");
reply = scanner.nextLine();
if ("pass".equals(reply) || "fail".equals(reply)) {
this.MOT = reply;
} else {
this.MOT = "invalid";
}
}
}
class Car extends Vehicle {
private String type;
private int age;
public Car() {
}
public Car(String licence, String MOT, String type, int age) {
super(licence, MOT);
this.type = type;
this.age = age;
}
public String getType() {
return type;
}
public int getAge() {
return age;
}
public void carAgeType(Scanner scanner) {
System.out.println("Enter age");
System.out.println("What type of car were you looking for :type: sports , town or 4X4");
String reply = scanner.next();
if ("sports".equals(reply) || "town".equals(reply) || "4X4".equals(reply)) {
this.type = reply;
} else {
this.type = "invalid";
}
System.out.println("And what sort of Age :type: between 1 and 10");
int years = scanner.nextInt();
if (years > 0 && years < 10) {
this.age = years;
} else {
this.age = -1;
}
}
}
请注意,我已经移除了原始代码中的HTML实体编码,以使代码更加清晰。如果您需要进一步的帮助,请随时提出。
英文:
class Main34 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Would you like to test a used car today? : y or n");
String val = scanner.nextLine();
if ("y".equals(val)) {
while (true) {
Car testCar = new Car();
testCar.drivingTest(scanner);
testCar.carAgeType(scanner);
break;
}
} else {
System.out.println("dang");
}
scanner.close();
}
}
class Vehicle {
private String licence, MOT;
public Vehicle() {
this("null", "null");
}
public Vehicle(String licence, String MOT) {
this.licence = licence;
this.MOT = MOT;
}
public String getLicence() {
return licence;
}
public String getMOT() {
return MOT;
}
public void drivingTest(Scanner scanner) {
System.out.println("great , do you have a driving licence :type: big, medium or small");
String reply = scanner.nextLine();
if ("big".equals(reply) || "medium".equals(reply) || "small".equals(reply)) {
this.licence = reply;
} else {
this.licence = "fail";
}
System.out.println("Great you passed your '" + licence + "' exam. Lets check the MOT : type: pass or fail");
reply = scanner.nextLine();
if ("pass".equals(reply) || "fail".equals(reply)) {
this.MOT = reply;
} else {
this.MOT = "invalid";
}
}
}
class Car extends Vehicle {
private String type;
private int age;
public Car() {
}
public Car(String licence, String MOT, String type, int age) {
super(licence, MOT);
this.type = type;
this.age = age;
}
public String getType() {
return type;
}
public int getAge() {
return age;
}
public void carAgeType(Scanner scanner) {
System.out.println("Enter age");
System.out.println("What type of car were you looking for :type: sports , town or 4X4");
String reply = scanner.next();
if ("sports".equals(reply) || "town".equals(reply) || "4X4".equals(reply)) {
this.type = reply;
} else {
this.type = "invalid";
}
System.out.println("And what sort of Age :type: between 1 and 10");
int years = scanner.nextInt();
if (years > 0 && years < 10) {
this.age = years;
} else {
this.age = -1;
}
}
}
Instead of creating Scanner object for every call, you just need to pass your existing scanner object to method.
Also instead of directly checking if ("y".equals(scanner.nextLine()))
, you need to check using some variable like this if ("y".equals(val))
Try above code, it will work fine.
答案2
得分: 0
> 当Scanner被关闭时,如果源实现了Closeable接口,则它将关闭其输入源。
这意味着当您的第一个方法关闭扫描仪时,它也会关闭输入源(System.in),这就是为什么您会收到异常以及为什么仅调用一个方法不会引发异常的原因。
只关闭主程序中的第一个扫描仪,这应该可以解决您的问题。
英文:
From the Oracle documentation on the Scanner class:
> When a Scanner is closed, it will close its input source if the source implements the Closeable interface.
This means that when your first method closes the scanner, it closes also the input (System.in) and that's why you get the exception and also why calling only one method doesn't throw the exception.
Just close only the first scanner you have in your main and it should solve your issue.
答案3
得分: 0
问题出在你使用了Scanner
与System.in
(这是一个静态的InputStream
)。当你在一个Scanner
上调用close()
时,它会自动关闭底层的InputStream
,在这种情况下就是System.in
。一旦System.in
被关闭,其他任何Scanner
都无法从中读取。
解决这个问题的方法是在每个函数中不要关闭Scanner
,只在main
方法中关闭它。
英文:
The problem comes from the fact that you use Scanner
with System.in
(which is a static InputStream
). Whenever you call close()
on a Scanner
, it will automatically close the underlying InputStream
, in this case System.in
. Once System.in
is closed, no other Scanner
can read from it.
The solution to that problem is to don't close the Scanner
in each of the functions and only do that in the main
method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论