在不同方法中插入不同的扫描器时出现异常。

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

Exception when I insert a different Scanner in different methods

问题

在主方法中,我创建了一个名为cls的对象,并调用了它的方法test。这个方法会调用另外两个方法(test1test2)。每个方法都有它自己的Scanner。

public static void main(String[] args) {
    Class2 cls = new Class2();
    cls.test();
}

Class2类如下:

public class Class2 {

    public Class2() {
    }

    public void test() {
        test2();
        test3();
    }

    public void test2() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String str = scanner.next();
        scanner.close();
    }

    public void test3() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入另一个字符串:");
        String str = sc.next();
        sc.close();
    }
}

执行后,我得到了一个异常:

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1478)
    at Class2.test3(Class2.java:25)
    at Class2.test(Class2.java:11)
    at Class1.main(Class1.java:12)

请问我该如何处理这个异常?通过在每个方法中保留不同的Scanner来处理它!

英文:

In the main method, I create an object cls and call its method test. This method will call two others methods (test1 and test2). Each one has its Scanner.

public static void main(String[] args) {
		Class2 cls = new Class2();
		cls.test();
	}

the Class2 is:

  public class Class2 {
 
    	public Class2() {
    	}
    	
	public void test()
	{
		test2();
		test3();
		
	}
	
	public void test2() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("give a String:");
	    String str = scanner.next(); 
	    scanner.close();
	}
	public void test3()
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("give another String:");
		String str = sc.next();
		sc.close();
		
	}

}

After execution, I got an exception

Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.Scanner.throwFor(Scanner.java:937)
	at java.base/java.util.Scanner.next(Scanner.java:1478)
	at Class2.test3(Class2.java:25)
	at Class2.test(Class2.java:11)
	at Class1.main(Class1.java:12)

How can I handle this exception please ? by keeping in each method a different scanner !

答案1

得分: 2

这是您纠正后的带有适当注释的代码。

Class2.java

import java.util.Scanner;

public class Class2 {
    /* 无需创建多个 Scanner 对象 */
    Scanner scan = new Scanner(System.in);

    public void test() {
        /* 为了在此类中运行方法,
         * 您必须使用 static 关键字或创建对象 */
        Class2 obj = new Class2();
        obj.test2();
        obj.test3();
        scan.close();
        /* 当您希望关闭 InputStream 时,
         * 应将 scan.close() 放置在此方法运行时
         * 您将在 Java Streams 中学到这个知识 */
    }

    public void test2() {
        System.out.println("请输入一个字符串:");
        String str = scan.nextLine();
    }

    public void test3() {
        System.out.println("请输入另一个字符串:");
        String str = scan.nextLine();
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        Class2 cls = new Class2();
        cls.test();
    }
}

为什么会发生错误?

: 当您的代码执行 test2() 方法时,它通过使用 scan.close() 关闭了扫描器的 InputStream,因此当执行 test3() 时,它无法再读取数据。解决方法是要么在 test3() 方法中关闭扫描器,要么在 test() 方法中关闭扫描器。

英文:

Here Is your rectified code with appropriate comments.

Class2.java

import java.util.Scanner;
public class Class2 {
    /*You dont have to create multiple scanner objects*/
    Scanner scan = new Scanner(System.in);

    public void test() {
        /*In order to run the methods in this class itself
        * you have to use static keyword or create object*/
        Class2 obj = new Class2();
        obj.test2();
        obj.test3();
        scan.close();
        /* As this method is run, scan.close() should be placed when you want to close InputStream
        * you will learn this in Java Streams*/
    }

    public void test2() {
        System.out.println("give a String:");
        String str = scan.nextLine();
    }
    public void test3() {
        System.out.println("give another String:");
        String str = scan.nextLine();

    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        Class2 cls = new Class2();
        cls.test();
    }
}

Why did the error occur?

Ans: When your code executes test2() method it closes the scanner InputStream in the ending by using scan.close(), hence when the test3() is executed it can no longer read data. The solution is that you either close scanner in the test3() method or in the test() method.

huangapple
  • 本文由 发表于 2020年7月24日 18:13:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63071528.html
匿名

发表评论

匿名网友

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

确定