NoSuchElementException 似乎被无故调用。

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

NoSuchElement Exception seems to be called for no reason

问题

I'm trying to make a program that will read a data file, sort of like this:

01 1 2 3 4 5
02 1 2 3 4 5
03 1 2 3 4 5

etc, and so far I have this code:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class fileReader {
    // Code...
}

I wrote a simple main method to try this out with the file PovertyData.txt, printing to output.txt:

import java.io.File;
import java.io.PrintWriter;

public class Main {
    // Code...
}

However, on line 75, inArray[i] = input.nextLine();, there is a NoSuchElement Exception with this error message:

Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
        at fileReader.splitFile(fileReader.java:75)
        at Main.main(Main.java:15)

Line 1651 of PovertyData.txt is the following:

08 03630 Edison School District 54-JT                                                  336       79       14 USSD13.txt 24NOV2014

I do not know why this is happening, as file has data in it, and all that statement is doing is reading each line of the file to an array. Please tell me why this is happening or how to fix it.

Thanks.

英文:

I'm trying to make a program that will read a data file, sort of like this:

01 1 2 3 4 5
02 1 2 3 4 5
03 1 2 3 4 5

etc, and so far I have this code:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class fileReader {
	int lineSpaceCount; // number of spaces per line in the input
	int outputSpaceCount; // number of spaces the output will use
	int[] includeData; // data value places that will be included into the output
	String returnText; // text that will be returned

	// CONSTRUCTORS
	public fileReader() {
		this.lineSpaceCount = 0;
		this.outputSpaceCount = 0;

		this.includeData = new int[outputSpaceCount];
	} // default constructor that sets everything to default values

	public fileReader(final int lineSpaceCount, final int outputSpaceCount) {
		this.lineSpaceCount = lineSpaceCount;
		this.outputSpaceCount = outputSpaceCount;

		this.includeData = new int[outputSpaceCount];
	} // constructor to set everything to custom values

	// SETTER METHODS
	public void setIncludeData(final int[] inputArray) {
		for (int i = 0; i < this.includeData.length; i++) {
			this.includeData[i] = inputArray[i];
		}
	}

	public void setOutputSpaceCount(final int outputSpaceCount) {
		this.outputSpaceCount = outputSpaceCount;
	}

	public void setLineSpaceCount(final int lineSpaceCount) {
		this.lineSpaceCount = lineSpaceCount;
	}

	// GETTER METHODS
	public int getLineSpaceCount() {
		return this.lineSpaceCount;
	}

	public int getOutputSpaceCount() {
		return this.outputSpaceCount;
	}

	public int[] getIncludeData() {
		return this.includeData;
	}

	// CUSTOM METHODS
	public String splitFile(final File file) throws IOException {
		int fileLength = 0;
		
		try (
			Scanner counter = new Scanner(new FileReader(file))
			) {
			while (counter.hasNextLine()) {
				counter.nextLine();
				fileLength++;
				} //counts number of lines in the file
			} //automatically closes the scanner

		final String[] inArray = new String[fileLength]; // array that will contain every line in the file
		String[] secArray = new String[this.lineSpaceCount]; // array that will contain every part of the line for inArray

		try (
			final Scanner input = new Scanner(file);
			) {
			for (int i = 0; i > -1; i++) {
				inArray[i] = input.nextLine(); //this is where the NoSuchElementException happens

				if (i > fileLength) {
					i = -1;
				} //if there are no more lines, end loop
			} //puts all the lines of the file into a massive array
		} //automatically closes the scanner

		final String[][] outArray = new String[inArray.length][this.outputSpaceCount]; // output array that will betranslated into text, has to be placed here for the .length attribute of inArray to be correct

		for (int h = 0; h < fileLength; h++) {
			secArray = inArray[h].split(" "); //splits the line into portions

			for (int k = 0; k < secArray.length; k++) {
				outArray[h][k] = secArray[k]; // h = rows, k = columns
			}
		} // nested loops to transfer information into the 2D array

		toText(outArray, fileLength);
		return this.returnText;
	} // massive method that splits a file into individual parts

	public void toText(final String[][] array, final int arrayLength) {
		for (int i = 0; i < arrayLength; i++) {
			for (int j = 0; j < includeData.length; j++ ) {
				this.returnText += array[i] [includeData[j]] + " ";
				} 
			this.returnText += "\n";
			} //method turns the 2D array into a neat output
			//if includeData = {0, 2} and outArray = {{0, 2, 3}{8, 9, 14}} then returnText will equal to 0 3 \n 8 14
		} //method that converts the 2D array into readable text
	}

I wrote a simple main method to try this out with the file PovertyData.txt, printing to output.txt.

import java.io.File;
import java.io.PrintWriter;

public class Main {
	public static void main(String []args) throws Exception {
		try (
			PrintWriter output = new PrintWriter("outputData.txt");
			) {
			File povertyFile = new File("PovertyData.txt"); //importing the file
			int [] includeData = {1, 3, 4, 5}; //all the data values needed for the output
		
			fileReader readFiles = new fileReader(7, 4);
			readFiles.setIncludeData(includeData);
			
			output.println(readFiles.splitFile(povertyFile));
			}
		catch (java.io.IOException ex) {
			System.out.println("Error: File does not exist");
			}
		}
	}

Here is an excerpt of PovertyData.txt (it's over 10,000 lines long)

01 00190 Alabaster City School District                                              31754     6475      733 USSD13.txt 24NOV2014  
01 00005 Albertville City School District                                            21522     4010     1563 USSD13.txt 24NOV2014  
01 00030 Alexander City City School District                                         17292     2719     1158 USSD13.txt 24NOV2014  
01 00060 Andalusia City School District                                               9044     1512      471 USSD13.txt 24NOV2014  
01 00090 Anniston City School District                                               22759     3304     1122 USSD13.txt 24NOV2014  
01 00100 Arab City School District                                                    8238     1491      250 USSD13.txt 24NOV2014  
01 00120 Athens City School District                                                 23494     3767      770 USSD13.txt 24NOV2014  
01 00180 Attalla City School District                                                 6019      971      283 USSD13.txt 24NOV2014  
01 00210 Auburn City School District                                                 57441     7120      994 USSD13.txt 24NOV2014  
01 00240 Autauga County School District                                              55246    10643     1855 USSD13.txt 24NOV2014  
01 00270 Baldwin County School District                                             195540    32652     6171 USSD13.txt 24NOV2014  
01 00300 Barbour County School District                                              14124     1769      759 USSD13.txt 24NOV2014  
01 00330 Bessemer City School District                                               27510     4765     2009 USSD13.txt 24NOV2014  
01 00360 Bibb County School District                                                 22512     3590     1096 USSD13.txt 24NOV2014  
01 00390 Birmingham City School District                                            212631    30718    12740 USSD13.txt 24NOV2014  
01 00420 Blount County School District                                               51246     9201     2218 USSD13.txt 24NOV2014  
01 00012 Boaz City School District                                                    9619     1742      464 USSD13.txt 24NOV2014  
01 00450 Brewton City School District                                                 5354      866      279 USSD13.txt 24NOV2014  
01 00480 Bullock County School District                                              10639     1565      684 USSD13.txt 24NOV2014  
01 00510 Butler County School District                                               20265     3494     1430 USSD13.txt 24NOV2014  
01 00540 Calhoun County School District                                              56502     9945     2162 USSD13.txt 24NOV2014  
01 00600 Chambers County School District                                             27696     4343     1348 USSD13.txt 24NOV2014  
01 00630 Cherokee County School District                                             26203     4163     1184 USSD13.txt 24NOV2014  
01 00188 Chickasaw City School District                                               6120     1028      443 USSD13.txt 24NOV2014  
01 00660 Chilton County School District                                              43951     7767     2503 USSD13.txt 24NOV2014  
01 00690 Choctaw County School District                                              13426     2160      616 USSD13.txt 24NOV2014  
01 00720 Clarke County School District                                               19459     3399     1146 USSD13.txt 24NOV2014  
01 00750 Clay County School District                                                 13486     2195      604 USSD13.txt 24NOV2014  
01 00780 Cleburne County School District                                             14631     2505      637 USSD13.txt 24NOV2014  
01 00810 Coffee County School District                                               18996     3302      719 USSD13.txt 24NOV2014  
01 00840 Colbert County School District                                              23763     3740      947 USSD13.txt 24NOV2014  
01 00870 Conecuh County School District                                              12887     2122      843 USSD13.txt 24NOV2014  
01 00900 Coosa County School District                                                10898     1557      439 USSD13.txt 24NOV2014  
01 00930 Covington County School District                                            22163     3506      745 USSD13.txt 24NOV2014  
01 00960 Crenshaw County School District                                             13986     2350      693 USSD13.txt 24NOV2014  
01 00990 Cullman City School District                                                14740     2313      555 USSD13.txt 24NOV2014  
01 01020 Cullman County School District                                              66044    11111     2083 USSD13.txt 24NOV2014  
01 01050 Dale County School District                                                 15140     2540      635 USSD13.txt 24NOV2014  
01 01080 Daleville City School District                                              11422     1883      573 USSD13.txt 24NOV2014  
01 01110 Dallas County School District                                               22104     4098     1860 USSD13.txt 24NOV2014  
01 01140 DeKalb County School District                                               56920    10543     2800 USSD13.txt 24NOV2014  
01 01170 Decatur City School District                                                55850     9364     2657 USSD13.txt 24NOV2014  
01 01200 Demopolis City School District                                               7172     1259      766 USSD13.txt 24NOV2014  
01 01230 Dothan City School District                                                 67258    11608     3349 USSD13.txt 24NOV2014  
01 01260 Elba City School District                                                    5282      830      322 USSD13.txt 24NOV2014  
01 01290 Elmore County School District                                               73612    12654     2392 USSD13.txt 24NOV2014  
01 01320 Enterprise City School District                                             27130     4813     1228 USSD13.txt 24NOV2014  
01 01350 Escambia County School District                                             32629     5146     1786 USSD13.txt 24NOV2014  
01 01380 Etowah County School District                                               60104    10265     1937 USSD13.txt 24NOV2014  
01 01410 Eufaula City School District                                                12952     2368      924 USSD13.txt 24NOV2014  

However, on line 75, inArray[i] = input.nextLine();, there is a NoSuchElement Exception with this error message:

Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
        at fileReader.splitFile(fileReader.java:75)
        at Main.main(Main.java:15)

Line 1651 of PovertyData.txt is the following:

08 03630 Edison School District 54-JT                                                  336       79       14 USSD13.txt 24NOV2014  

I do not know why this is happening, as "file" has data in it and all that statment is doing is reading each line of the file to an array. Please tell me why this is happening or how to fix it.

Thanks.

答案1

得分: 1

你在for循环中有一个bug,当从文件中读取时。
而不是修复它,有一种更简单的方法可以在Java中读取文件的所有行。

List<String> allLines = Files.readAllLines(file.toPath());
String[] inArray = new String[allLines.size()];
allLines.toArray(inArray);
英文:

You have a bug in for loop, while reading from file.
Instead of fixing it, there is a simpler way to read all lines from file in Java.

List&lt;String&gt; allLines = Files.readAllLines(file.toPath());
String[] inArray = new String[allLines.size()];
allLines.toArray(inArray); 

huangapple
  • 本文由 发表于 2020年8月1日 03:04:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63197731.html
匿名

发表评论

匿名网友

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

确定