Results 1 to 6 of 6
- 06-06-2012, 11:26 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
Scanner not reading every character in a file?
So I'm making a new game and i need it to read a file in order to build the level your character will walk around in. I have it pretty much done, except it seems to only read half of the numbers in the text file. Why is this not working?
public class level extends JPanel{
private int[][] layout;
public level() throws IOException{
setPreferredSize(new Dimension(1000, 650));
layout = new int[20][13];
int i=0, j=0;
Scanner scan = new Scanner(new File("level.txt"));
while(scan.hasNext()){
if(scan.nextInt()==0){
i=0;
j++;
}
else{
layout[i][j] = scan.nextInt();
i++;
}
}
}
this is what's in the text file:
2 2 2 2 1 1 1 2 2 2 2 3 3 3 2 2 2 1 2 2 0
2 2 2 1 1 1 2 2 2 2 2 3 3 3 2 2 2 1 1 1 0
2 2 2 2 2 2 2 2 2 2 2 3 3 3 2 2 2 2 2 1 0
2 2 2 2 4 4 4 4 4 2 2 3 3 3 2 2 2 2 2 2 0
2 2 2 2 4 4 2 4 4 2 2 3 3 3 2 2 2 2 2 2 0
2 2 2 4 4 4 4 4 4 4 2 3 3 3 2 2 2 2 2 2 0
2 2 2 2 4 4 4 4 4 4 2 3 3 3 2 2 2 1 2 1 0
2 2 2 4 4 4 4 4 4 4 2 3 3 3 2 2 2 1 1 1 0
2 2 2 2 2 4 4 4 4 2 2 3 3 3 2 2 2 2 1 1 0
2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 0
2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 0
2 2 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 0
2 2 2 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0
- 06-06-2012, 11:36 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
Re: Scanner not reading every character in a file?
It is reading all the numbers in the file...note how you are calling scanner.nextInt() twice - once when you check the conditional, and another time in the code when that conditional evaluates to false.
-
Re: Scanner not reading every character in a file?
You are reading the scanner twice in the while loop, and ignoring the results from the first read -- inside of the if block.
Myself, I'd scan each line using hasNextLine() inside of a while loop, and then use a separate Scanner for each line in its own while loop, using hasNextInt(), and remembering to close each scanner when I'm done with it.Last edited by Fubarable; 06-06-2012 at 11:48 PM.
- 06-07-2012, 02:13 AM #4
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
Re: Scanner not reading every character in a file?
oooh thank you! I understand what's going on now. Here's my appended code, not a lot of change but it made it work:
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class level extends JPanel{
private int[][] layout;
public level() throws IOException{
setPreferredSize(new Dimension(1000, 650));
layout = new int[20][13];
int i=0, j=0, nextInt = 0;
Scanner scan = new Scanner(new File("level.txt"));
while(scan.hasNext()){
nextInt = scan.nextInt();
if(nextInt==0){
i=0;
j++;
}
else{
layout[i][j] = nextInt;
i++;
}
}
}
-
Re: Scanner not reading every character in a file?
A word of caution, always try to have your Scanner#hasNextXXX() method match your call to Scanner#nextXXX(). So in other words, if in the body of your while block you are extracting scan.nextInt(), in the while boolean condition, check for scan.hasNextInt(). Likewise before you extract scan.nextLine(), you check for scan.hasNextLine(), or if you will call scan.nextDouble() you first check for scan.hasNextDouble(), or if scan.next(), you first check for scan.hasNext(). This can protect you against some errors.
- 06-07-2012, 07:57 AM #6
Re: Scanner not reading every character in a file?
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
Reading file problem using Scanner
By nfsmwbe in forum New To JavaReplies: 18Last Post: 01-04-2012, 03:26 PM -
Problem reading a text file into a two-dimensional character array
By wvu5004 in forum New To JavaReplies: 5Last Post: 03-12-2011, 01:04 AM -
How To Retain Entity Reference Character While Reading XML
By leon850515 in forum XMLReplies: 0Last Post: 01-28-2011, 11:20 AM -
Reading ASCII / Unicode character in Java
By NGRaju in forum New To JavaReplies: 2Last Post: 09-16-2008, 10:02 PM -
reading text character by character
By bugger in forum New To JavaReplies: 2Last Post: 11-09-2007, 08:54 PM
Bookmarks