Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Simple scanner problem

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple scanner problem

    I'm using this method to read records from a file in my root project directory, storing them into an array of objects.

    example of a line of text:
    Texas Austin TX19759614Southwest 5

    this is the method i'm using:

        public void readStates() {
     
            File file = new File("States.Fall2009.txt");
     
            try {
     
                Scanner console = new Scanner(file);
                Scanner lineTokenizer;
                int lineNum = 0;
     
                while (console.hasNextLine()) {
                    lineTokenizer = new Scanner(console.nextLine());
                    lineNum++;
     
                    if (lineTokenizer.hasNext())
                        a[lineNum].name = lineTokenizer.next();
                    else {
                        System.out.printf("line %d: State field is invalid", lineNum);
                        continue;
                    }
     
    etc...etc...etc...

    can anyone help me out with this?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Simple scanner problem

    I'd say it's cause a hasn't been declared. Other than that, I can't really see anything else wrong (I'm assuming you're closing the curly brackets at the bottom somewhere, and have the appropriate catch block).

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple scanner problem

    i feel like i already have, this time i'll include all of what i have so far, here it is:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    /**
     *
     * @author Ryan
     */
     
    class State {
     
        public String name;
        public String capital;
        public String symbol;
        public int number;
        public String location;
        public int locationNumber;
     
        public State(String nm, String cap, String sym, int num,
                String loc, int locNum) {
            name = nm;
            capital = cap;
            symbol = sym;
            number = num;
            location = loc;
            locationNumber = locNum;
        }
     
    }
     
    class ClassDataArray {
        private State[] a;
        private int nElems;
     
        public ClassDataArray(int max) {
            a = new State[max];
            nElems = 0;
        }
     
        public void print() {
            int i = 0;
            a = new State[50];
     
            for (i=0; i<a.length; i++)
                System.out.printf("%15s%15s%15s%3s%8d%16s%2d", a[i].name, a[i].capital, a[i].symbol, a[i].number, a[i].location, a[i].locationNumber);
        }
     
        public void readStates() {
     
            File file = new File("States.Fall2009.txt");
     
            try {
     
                Scanner console = new Scanner(file);
                Scanner lineTokenizer;
                int lineNum = 0;
     
                while (console.hasNextLine()) {
                    lineTokenizer = new Scanner(console.nextLine());
                    lineNum++;
     
                    if (lineTokenizer.hasNext())
                        a[lineNum].name = lineTokenizer.next();
                    else {
                        System.out.printf("line %d: State field is invalid", lineNum);
                        continue;
                    }
     
                    if (lineTokenizer.hasNext())
                        a[lineNum].capital = lineTokenizer.next();
                    else {
                        System.out.printf("line %d: Capital field is invalid", lineNum);
                        continue;
                    }
     
                    if (lineTokenizer.hasNext())
                        a[lineNum].symbol = lineTokenizer.next();
                    else {
                        System.out.printf("line %d: State symbol field is invalid", lineNum);
                        continue;
                    }
     
                    if (lineTokenizer.hasNextInt())
                        a[lineNum].number = lineTokenizer.nextInt();
                    else {
                        System.out.printf("line %d: State number field is invalid", lineNum);
                        continue;
                    }
     
                    if (lineTokenizer.hasNext())
                        a[lineNum].location = lineTokenizer.next();
                    else {
                        System.out.printf("line %d: State location field is invalid", lineNum);
                        continue;
                    }
     
                    if (lineTokenizer.hasNextInt())
                        a[lineNum].locationNumber = lineTokenizer.nextInt();
                    else {
                        System.out.printf("line %d: Location number field is invalid", lineNum);
                        continue;
                    }
                }
             } catch (FileNotFoundException e) {
                 e.printStackTrace();
             }
     
        }
    }
     
    public class Main {
     
        public static void main(String[] args) {
            ClassDataArray arr;
            arr = new ClassDataArray(50);
     
            arr.readStates();
            arr.print();
        }
    }

    thank you for your help

  4. #4
    Junior Member
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple scanner problem

    "run:
    Exception in thread "main" java.lang.NullPointerException
    at project1.ClassDataArray.readStates(Main.java:65)
    at project1.Main.main(Main.java:119)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    "

    that is my error code and line 65 is
    a[lineNum].name = lineTokenizer.next();
    and line 119 is
    arr.readStates();

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Simple scanner problem

    Ahh, i see your problem. You initialized an array of states, but the array contains all nulls. Change that line of code to this (line 65):
    a[lineNum].name = new State();
    a[lineNum].name = lineTokenizer.next();

    I also would add an empty constructor to the State class.

    Another thing I noticed is that you advanced lineNum before putting stuff into the state. This will give you an error if you try reading in max # of states.

Similar Threads

  1. network scanner
    By vivek494818 in forum Java Networking
    Replies: 0
    Last Post: August 17th, 2009, 11:07 PM
  2. simple problem w/ appelets which i cant figure out
    By JavaGreg in forum Java Applets
    Replies: 7
    Last Post: August 15th, 2009, 07:22 PM
  3. reading a char with SCANNER
    By lotus in forum Java SE APIs
    Replies: 6
    Last Post: July 29th, 2009, 05:03 AM
  4. [SOLVED] Problem in Coin-counter with scanner class
    By coccoster in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 25th, 2009, 08:46 AM
  5. Replies: 1
    Last Post: May 13th, 2008, 08:08 AM