简体   繁体   English

Java 在读取文件时出现 Scanner 和 hasNextLine() 问题

[英]Java issues with Scanner and hasNextLine() while reading a file

I am having an issue with this unfinished program.我对这个未完成的程序有疑问。 I do not understand why this returns a "no Line found exception" when I run it.我不明白为什么当我运行它时会返回“未找到行异常”。 I have a while loop set up whose purpose is checking for this but I have done something wrong.我设置了一个 while 循环,其目的是检查这一点,但我做错了什么。 I am trying to store information from a file into a 2d array for class.我正在尝试将文件中的信息存储到类的二维数组中。

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;
import java.util.Arrays;

public class LabProgram {
   public static void main(String[] args) throws IOException {
      Scanner scnr = new Scanner(System.in);
      int NUM_CHARACTERS = 26;      // Maximum number of letters
      int MAX_WORDS = 10;           // Maximum number of synonyms per starting letter
      String userWord = (scnr.next()) + ".txt"; //Get word user wants to search
      char userChar = scnr.next().charAt(0); //Get char user wants to search
      String[][] synonyms = new String[NUM_CHARACTERS][MAX_WORDS];  // Declare 2D array for all synonyms
      String[] words = new String[MAX_WORDS]; // The words of each input line
      
      File aFile = new File(userWord);
      
      Scanner inFile = new Scanner(aFile);
      
      while(inFile.hasNextLine()) {
         for(int i = 0; i < synonyms.length; i++) {
            words = inFile.nextLine().trim().split(" ");
            for(int wordCount = 0; wordCount < words.length; wordCount++) {
               synonyms[i][wordCount] = words[wordCount];
            }
         }
      }
      
   }
}

The issue is with this for loop:问题在于这个 for 循环:

for (int i = 0; i < synonyms.length; i++) {
    words = inFile.nextLine().trim().split(" ");
    ....
}

You're iterating from i=0 upto synonym.length-1 times, but that file does not have these much lines, so, as soon as your file is out of lines but the for loop has scope to iterate more, the inFile.nextLine() gets no line and thus throw the exception.您正在从i=0迭代到synonym.length-1次,但该文件没有这么多行,因此,一旦您的文件超出行但 for 循环有更多迭代范围, inFile.nextLine()没有行,因此抛出异常。

I don't know what you are exactly doing here or want to achieve through this code, but this is what causing you the trouble.我不知道你在这里到底在做什么或想通过这段代码实现什么,但这就是给你带来麻烦的原因。

Hope that answers your query.希望能解答您的疑问。

Basically your problem is that you're only checking hasNextLine() before the for loop starts, and you're actually getting the next line on every iteration of the loop.基本上你的问题是你只在for循环开始之前检查hasNextLine() ,你实际上是在循环的每次迭代中获得下一行。 So if you run out of lines while in the middle of your for loop an exception is thrown.因此,如果在for循环中间用完行,则会引发异常。

I'm actually not quite sure what your code is supposed to do but at the very least you need to add a hasNextLine() check every time before you actually run nextLine() to avoid errors like this.我实际上不太确定你的代码应该做什么,但至少你需要在每次实际运行nextLine()之前添加一个hasNextLine()检查以避免这样的错误。

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

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