简体   繁体   English

Java扫描仪异常

[英]A Java Scanner Exception

I am trying to write a program where I take a textfile and copy it to another file. 我试图编写一个程序,将文本文件复制到另一个文件中。 In this other file I want one word on the first line, two words on the second, three on the third and so on. 在另一个文件中,我希望第一行有一个单词,第二行有两个单词,第三行有三个单词,依此类推。

I am having some trouble with the Scanner class however. 我在使用Scanner类时遇到了一些麻烦。 In the program below I keep getting a NoSuchElementException for line 14. I thought this was because I closed the Scanner in the while loop or something but even when I left out 'in.close()' I kept getting the same error. 在下面的程序中,我继续获得第14行的NoSuchElementException。我认为这是因为我在while循环中关闭了Scanner之类的东西,但是即使我省略了“ in.close()”,我仍然遇到相同的错误。

Can anybody help me with this? 有人可以帮我吗?

Thanks in advance. 提前致谢。

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

public class WordPyramid {
   public static void main(String[] args) throws FileNotFoundException {
      File inputFile = new File(args[0]);
      Scanner in = new Scanner(inputFile);
      PrintWriter out = new PrintWriter(args[1]);
      int s = 1;
      int i = 0;
      while (in.hasNext()) {
        if (s >= i) {
           for (i = 1; i <= s; i++) {
              out.print(in.next());
              out.print(" ");
            }
        out.println("");
        s++;
        }
    }
  in.close();
  out.close();
  }
}

A NoSuchElementException is thrown when there is no next() element. 如果没有next()元素,则抛出NoSuchElementException While you check to see if the file hasNext() at the start of each layer of the pyramid, you also need to check it before you call next() in the for loop. 在检查金字塔的每一层的开头是否具有hasNext()文件的同时,还需要在for循环中调用next()之前对其进行检查。 Your exception is thrown in the for loop because the next layer of the pyramid may require a larger number of words than remains in the file causing next() to try and get an element that is not there. 您的异常会在for循环中抛出,因为金字塔的下一层可能需要的单词数量比文件中剩余的单词数量多,从而导致next()尝试获取不存在的元素。

To fix it, wrap the interior of you for loop with if(in.hasNext()) . 要修复它,请使用if(in.hasNext())将您的内部包裹起来for循环。

hasNext() checks if there is one more token in the scanner's stream. hasNext()检查扫描器流中是否还有一个令牌。 You are checking if there is one more token, and then assuming there are even more than one in your for loop. 您正在检查是否还有一个令牌,然后假设您的for循环中甚至有多个令牌。 I would modify your for loop to look like this: 我将修改您的for循环,如下所示:

for (i = 1; i <= s && in.hasNext(); i++)

I would like to suggest that your loops are perhaps over-complicated. 我想建议您的循环可能过于复杂。

Here is what I think is an easier answer, and avoids your exception: 我认为这是一个更简单的答案,并且避免了您的例外情况:

File inputFile = new File(args[0]);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(args[1]);
int s = 1;
int i = 0;
while (in.hasNext()) {
  out.print(in.next() + " ");
  i++;
  if (i == s)
  {
     // Start the next line
     out.println();
     s++;
     i = 0;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM