Hello and Happy holidays to all. I am new here, so sorry to bother you. I'm working on a project for a class and have run into a bit of a snag. I have a method that reads integer values from a text file and stores them into an array for use later on. The problem is, every time I try to run the program, I get an input mismatch exception, I'm scanning for integers and the only thing in the file is integers so I don't understand why this isn't working. I have tried commenting out the loop and putting generic System.out.print(scanner.next()); statements in to see what it is actually reading. when I try that it prints the title of the file "testit", but after that it finds nothing hence the NoSuchElementException. So I dont think the file is reading properly or something. any insight on this would be very helpful.

OG method:

public int[][] readData() 
		int[][] dataArray = new int[200][4];


                         for(int i = 0; i<= 200; i++) 

				dataArray[i][0] = fileRead.nextInt();
				dataArray[i][1] = fileRead.nextInt();
				dataArray[i][2] = fileRead.nextInt();
				dataArray[i][3] = fileRead.nextInt();

		return dataArray;

**fileRead is the Scanner

1234 300 500 200
1235 200 500 300
1236 200 300 500
1237 500 200 300
1238 500 300 200

8 Years
Discussion Span
Last Post by BestJewSinceJC

No l33t speak and use of code tags on the very first post; this has given me happiness indeed.

Anyways, the problem with your approach is that you attempt to read in `int' from the file but don't test for it i.e. instead of fileRead.hasNext() test for fileRead.hasNextInt() . Also, your code will fail if any of the lines have less than four tab separated integer strings. Consider having a test for int before reading one.

IMO, reading in a single line from the file and parsing it manually gives you much more control and flexibility in your implementation than using fileRead.nextInt() . To prove this point, try replacing one of your integer strings with a alphanumeric character. Also, intermixing the file reading logic along with the parsing logic / file format logic is bad in itself; consider having a separate class process each line of file separately so that if you need to change the format, the impact is minimalistic.


Ahh thank you, thank you. I am new to java and didn't realize you could read the whole line at once. I re-wrote the method to just read in the line strings and set up a separate class to convert them into their respected integers like you suggested. This made things alot cleaner. I also found out that farther up in the class I had written my scanner wrong, which was why it wasn't finding anything in the file to begin with.


In case this is helpful (for what you mentioned above)

//By default, can get input typed from the keyboard
//This would change if System.in was redirected
Scanner keyboard = new Scanner(System.in);

//Get input from a file
Scanner fileInput = new Scanner(new FileInputStream("yourFile.txt"));

Also, following up on what S.o.s said, keep in mind that there is a method called parseInt (use: Integer.parseInt(String anIntString);). This method allows you to attempt to parse Strings into Integers. For example, if you read in everything from your file as a String, but you expect that they are actually ints, you could use

int myInt = Integer.parseInt(StringReadIn);

Also, for testing (and for good implementation) you should surround that with a try catch and report any errors (parseInt will throw an exception if the String you pass in can't be converted to an int).

What I said above is just another thing that you can do. The way S.O.S suggested, with using hasNextInt(), is probably better because it will tell you right off the bat whether or not whats next in the file is an integer or not.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.