Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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_Input - Fatal编程技术网

使用Java在循环内获取输入

使用Java在循环内获取输入,java,input,Java,Input,我试图在循环中使用scanner.nextLine(),但出现了一个异常。 问题出在代码的这一部分 while(!sentence.equals("quit")){ dealWithSentence(sentence, voc); System.out.println("Enter your sentence:"); sentence = scanner.nextLine(); } 有一个例外: 线程“main”java.

我试图在循环中使用scanner.nextLine(),但出现了一个异常。 问题出在代码的这一部分

        while(!sentence.equals("quit")){
        dealWithSentence(sentence, voc);
        System.out.println("Enter your sentence:");
        sentence = scanner.nextLine();
    }
有一个例外:

线程“main”java.util.NoSuchElementException中出现异常:未找到任何行 位于java.util.Scanner.nextLine(未知源) 位于il.ac.tau.cs.sw1.ex4.SpellingCorrector.main(SpellingCorrector.java:34)

这是我的完整方法代码:

    public static void main(String[] args) throws Exception{
    Scanner scanner = new Scanner(System.in);
    String filePath = scanner.nextLine();
    if (filePath.contains(" ")){
        scanner.close();
        throw new Exception("[ERROR] the file path isnt correct");
    }

    File file = new File(filePath);
    String[] voc = scanVocabulary(new Scanner(file));
    if (voc == null)
    {
        scanner.close();
        throw new Exception("[ERROR] the file isnt working");

    }


    System.out.println("Read " + voc.length + " words from " + file.getName());

    System.out.println("Enter your sentence:");

    String sentence = scanner.nextLine();

    while(!sentence.equals("quit")){
        dealWithSentence(sentence, voc);
        System.out.println("Enter your sentence:");
        sentence = scanner.nextLine();
    }
    scanner.close();
Scanner.nextLine()的工作原理如下

   String s = "Hello World! \n 3 + 3.0 = 6.0 true ";

   // create a new scanner with the specified String Object
   Scanner scanner = new Scanner(s);

   // print the next line
   System.out.println("" + scanner.nextLine());

   // print the next line again
   System.out.println("" + scanner.nextLine());

   // close the scanner
   scanner.close();
   }
这将为您提供以下输出

Hello World!
3 + 3.0 = 6.0 true 
基本上,它开始扫描并跳过,直到第一个新行字符,然后返回它跳过的任何内容,直到输出为止。在您的情况下,如果您只有一个句子,而其中根本没有新行(\n),它将跳过整个长度,并且永远找不到新行。因此抛出异常。。。在句子中间添加一个新行字符,看看异常是否消失

信用卡转到:

扫描仪。nextLine()的工作原理如下

   String s = "Hello World! \n 3 + 3.0 = 6.0 true ";

   // create a new scanner with the specified String Object
   Scanner scanner = new Scanner(s);

   // print the next line
   System.out.println("" + scanner.nextLine());

   // print the next line again
   System.out.println("" + scanner.nextLine());

   // close the scanner
   scanner.close();
   }
这将为您提供以下输出

Hello World!
3 + 3.0 = 6.0 true 
基本上,它开始扫描并跳过,直到第一个新行字符,然后返回它跳过的任何内容,直到输出为止。在您的情况下,如果您只有一个句子,而其中根本没有新行(\n),它将跳过整个长度,并且永远找不到新行。因此抛出异常。。。在句子中间添加一个新行字符,看看异常是否消失


使用
scanner.nextLine()

否则,扫描仪可能没有任何元素,无法提供下一行

通常,您将在循环中读取输入,例如:

while (scanner.hasNextLine()) {
  System.out.println("Line: " + scanner.nextLine());
}

在使用
scanner.nextLine()
之前,请检查
scanner.hasNextLine()

否则,扫描仪可能没有任何元素,无法提供下一行

通常,您将在循环中读取输入,例如:

while (scanner.hasNextLine()) {
  System.out.println("Line: " + scanner.nextLine());
}

哦,这更好。使用这个。但问题是没有下一行,我想从用户那里获取它作为输入。然后,您必须在输入蒸汽上循环,每次终止一行时,
扫描仪
将返回一个
nextLine()
。您的意思是每个循环都需要包含Scanner sc=new Scanner(System.in);?因为我已经试过了,但它也不起作用。不,只需创建一个
Scanner
实例,让它在循环中读取用户输入。要防止出现
NoTouchElementException
,请在调用
nextLine()
.Op之前使用
hasNext()
检查扫描仪,这样更好。使用这个。但问题是没有下一行,我想从用户那里获取它作为输入。然后,您必须在输入蒸汽上循环,每次终止一行时,
扫描仪
将返回一个
nextLine()
。您的意思是每个循环都需要包含Scanner sc=new Scanner(System.in);?因为我已经试过了,但它也不起作用。不,只需创建一个
Scanner
实例,让它在循环中读取用户输入。要防止出现
NoTouchElementException
,请在调用
nextLine()
之前使用
hasNext()
检查扫描仪。