Exception thrown while calling scanner more than once. "Exception in thread main" java.util.NoSuchElementException: No line found"

huangapple go评论108阅读模式
英文:

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 &quot;main&quot; 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(&quot;Would you like to test a used car today? : y or n&quot;);
if (&quot;y&quot;.equals(scanner.nextLine())){
while (true) {
Car testCar = new Car();
testCar.drivingTest();
testCar.carAgeType();
break;
}
}else{
System.out.println(&quot;dang&quot;);
}
scanner.close();
}
}
import java.util.Scanner;
public class Vehicle {
private String licence, MOT;
public Vehicle(){
this(&quot;null&quot;, &quot;null&quot;);
}
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(&quot;great , do you have a driving licence :type: big, medium or small&quot;);
String reply = DTScan.nextLine();
if (&quot;big&quot;.equals(reply) || &quot;medium&quot;.equals(reply) || &quot;small&quot;.equals(reply)) {
this.licence = reply;
} else {
this.licence = &quot;fail&quot;;
}
System.out.println(&quot;Great you passed your &#39;&quot; + licence + &quot;&#39; exam. Lets check the MOT : type: pass or fail&quot;);
reply = DTScan.nextLine();
if (&quot;pass&quot;.equals(reply) || &quot;fail&quot;.equals(reply)) {
this.MOT = reply;
} else {
this.MOT = &quot;invalid&quot;;
}
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(&quot;What type of car were you looking for :type: sports , town or 4X4&quot;);
ATScan.nextLine();
String reply = ATScan.nextLine();
if (&quot;sports&quot;.equals(reply) || &quot;town&quot;.equals(reply) || &quot;4X4&quot;.equals(reply)) {
this.type = reply;
} else {
this.type = &quot;invalid&quot;;
}
System.out.println(&quot;And what sort of Age :type: between 1 and 10&quot;);
int years = ATScan.nextInt();
if (years &gt; 0 &amp;&amp; years &lt; 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(&quot;Would you like to test a used car today? : y or n&quot;);
String val = scanner.nextLine();
if (&quot;y&quot;.equals(val)) {
while (true) {
Car testCar = new Car();
testCar.drivingTest(scanner);
testCar.carAgeType(scanner);
break;
}
} else {
System.out.println(&quot;dang&quot;);
}
scanner.close();
}
}
class Vehicle {
private String licence, MOT;
public Vehicle() {
this(&quot;null&quot;, &quot;null&quot;);
}
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(&quot;great , do you have a driving licence :type: big, medium or small&quot;);
String reply = scanner.nextLine();
if (&quot;big&quot;.equals(reply) || &quot;medium&quot;.equals(reply) || &quot;small&quot;.equals(reply)) {
this.licence = reply;
} else {
this.licence = &quot;fail&quot;;
}
System.out.println(&quot;Great you passed your &#39;&quot; + licence + &quot;&#39; exam. Lets check the MOT : type: pass or fail&quot;);
reply = scanner.nextLine();
if (&quot;pass&quot;.equals(reply) || &quot;fail&quot;.equals(reply)) {
this.MOT = reply;
} else {
this.MOT = &quot;invalid&quot;;
}
}
}
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(&quot;Enter age&quot;);
System.out.println(&quot;What type of car were you looking for :type: sports , town or 4X4&quot;);
String reply = scanner.next();
if (&quot;sports&quot;.equals(reply) || &quot;town&quot;.equals(reply) || &quot;4X4&quot;.equals(reply)) {
this.type = reply;
} else {
this.type = &quot;invalid&quot;;
}
System.out.println(&quot;And what sort of Age :type: between 1 and 10&quot;);
int years = scanner.nextInt();
if (years &gt; 0 &amp;&amp; years &lt; 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 (&quot;y&quot;.equals(scanner.nextLine())) , you need to check using some variable like this if (&quot;y&quot;.equals(val))

Try above code, it will work fine.

答案2

得分: 0

Oracle关于Scanner类的文档中:

> 当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

问题出在你使用了ScannerSystem.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.

huangapple
  • 本文由 发表于 2020年8月21日 18:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63520755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定