Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用扫描仪的非接触元件_Java_Java.util.scanner_Nosuchelementexception - Fatal编程技术网

Java 使用扫描仪的非接触元件

Java 使用扫描仪的非接触元件,java,java.util.scanner,nosuchelementexception,Java,Java.util.scanner,Nosuchelementexception,在此之后,当我关闭扫描仪并再次使用扫描仪读取另一个输入时,它会抛出一个错误:NoTouchElementException。 请在这方面指导我 I have declared two strings and reading the input using Scanner(System.in). 当您使用扫描仪时,它也会关闭输入流,您正在再次使用它,但它已关闭,因此当您尝试再次使用扫描仪时,在流中找不到打开的系统 无需关闭扫描程序,因为它实现了自动关闭接口。从java 7开始,您应该在try w

在此之后,当我关闭扫描仪并再次使用扫描仪读取另一个输入时,它会抛出一个错误:NoTouchElementException。 请在这方面指导我

I have declared two strings and reading the input using Scanner(System.in).

当您使用扫描仪时,它也会关闭输入流,您正在再次使用它,但它已关闭,因此当您尝试再次使用
扫描仪
时,在流中找不到打开的
系统

无需关闭扫描程序,因为它实现了自动关闭接口。从java 7开始,您应该在try with resources中声明资源。如果关闭扫描仪是一个问题

import java.util.Scanner;
import java.io.*;  

public class NumericInput

{
  public static void main(String[] args)
  {
    // Declarations
    Scanner in = new Scanner(System.in);
    String string1;
    String string2;

   // Prompts 
    System.out.println("Enter the value of the First String .");
   // Read in values  
    string1 = in.nextLine();
    // When i am commenting below line(in.close) code is working properly. 
    in.close();
    Scanner sc = new Scanner(System.in);
    System.out.println("Now enter another value.");
    string2 = sc.next();
    sc.close();

    System.out.println("Here is what you entered: ");
    System.out.println(string1 + " and " + string2);
  }

}
try(Scanner in = new Scanner(System.in); Scanner sc = new Scanner(System.in)){
 // do stuff here without closing
}

 catch(Exception){
  e.printStackTrace();
 }