Help with file input
Hello,
Please help...
I am trying to read a file I named "SudokuPuzzle1.txt"
9
400000003
009000000
000100070
000010800
000509000
001020000
030005000
000000700
700000008
The first integer in the file is the size of the double array.
The next 9 lines of integers are values of a double array.
I am able to read the first integer and get an error reading the next line. Here's what I have so far:
// Create and associate the Scanner object with the input source.
Scanner inFile =new Scanner (new FileReader("SudokuPuzzle1.txt"));
int arraySize;// variable arraySize
arraySize = inFile.nextInt();// arraySize obtained from first line of input file
int [][] puzzleFileBoard =newint[arraySize][arraySize];// dynamic array instantiated during program execution
System.out.println("Array size is " + arraySize +" by " + arraySize +".");// print sudoku puzzle array size
while (inFile.nextInt() != -1){
for (int i = 0; i == arraySize; i++){
for (int j = 0; j == arraySize; j++){
puzzleFileBoard[i][j] = inFile.nextInt();
}
Here is my error message:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:817)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
[2224 byte] By [
aiki985a] at [2007-11-26 18:30:47]

Try sticking an inFile.nextLine() in there after you read each int. It may not be reading the carriage returns at the end of each line. Reading the nextLine will catch the carriage return. But if it *is* reading the carriage return, then this'll mess it up.
Apart from that, you're also not reading the file correctly. Technically - the next int after 9 in the file that you have as shown is 400000003 ... not 4. I think you actually want to grab the next character out of the file, convert it into an int, and stick it in the array. But as it stands now, you'll be trying to read the file past the length of the file.
Actually ... looking at the error message closer, I don't think there is any issue with the carriage return.. I think it's simply that you're trying to read beyond the file.
> Try sticking an inFile.nextLine() in there after you
> read each int. It may not be reading the carriage
> returns at the end of each line. Reading the
> nextLine will catch the carriage return. But if it
> *is* reading the carriage return, then this'll mess
> it up.
>
> Apart from that, you're also not reading the file
> correctly. Technically - the next int after 9 in the
> file that you have as shown is 400000003 ... not 4.
> I think you actually want to grab the next character
> out of the file, convert it into an int, and stick
> it in the array. But as it stands now, you'll be
> trying to read the file past the length of the
> file.
>
> Actually ... looking at the error message closer, I
> don't think there is any issue with the carriage
> return.. I think it's simply that you're trying to
> read beyond the file.
o.k. thanks. I'm going to try and put a space between all integers...maybe put them all on one line.