Results 1 to 6 of 6
  1. #1
    Brandonhspace is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  4. #4
    Brandonhspace is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default 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++;
    }
    }
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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

  1. Reading file problem using Scanner
    By nfsmwbe in forum New To Java
    Replies: 18
    Last Post: 01-04-2012, 03:26 PM
  2. Replies: 5
    Last Post: 03-12-2011, 01:04 AM
  3. Replies: 0
    Last Post: 01-28-2011, 11:20 AM
  4. Reading ASCII / Unicode character in Java
    By NGRaju in forum New To Java
    Replies: 2
    Last Post: 09-16-2008, 10:02 PM
  5. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 08:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •