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 4 of 4

Thread: need help inputting values into an array

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default need help inputting values into an array

    hi im trying to input values from a txt file gn.txt into an array guestNumber
    this is my code so far


       Scanner cc = new Scanner( new File("CC.txt") );
              Scanner sn = new Scanner( new File("sn.txt") );
              Scanner ng = new Scanner( new File("gn.txt") );
              String line ;
              String line2 ;
              String line3 ;
              System.out.println("course code"+" " + "Student Number" +" "+"Number of guests");
     
              int lineCount = 0;  
     
     BufferedReader br2 = new BufferedReader(new FileReader("gn.txt"));  
     while ((line = br2.readLine()) != null) {  
     lineCount++;  
     
     } 
     System.out.println(lineCount);
    double guestNumbers[] = new double[lineCount];
     
           for(int c=0; c< lineCount; c++)
    {
     
                 double e=Double.parseDouble(br2.readLine());
                   System.out.println(e);
                guestNumbers[c]= e;
    }                               
              while( cc.hasNext() && sn.hasNext() && ng.hasNext() ){
                line = cc.nextLine();
                line2 = sn.nextLine();
                line3 = ng.nextLine();
     
     
                int i=Integer.parseInt(line);
                int i2=Integer.parseInt(line2);
               double i3=Double.parseDouble(line3);
     
                System.out.println(i + "         " + i2 + "         " + i3);
     
     
             }
     
     
    System.out.println(guestNumbers[0]);
    System.out.println(guestNumbers[1]);
    System.out.println(guestNumbers[2]);
    System.out.println(guestNumbers[3]);
     
     
             cc.close();
            sn.close();
            ng.close();



    when i run the program im getting a null pointer exception


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help inputting values into an array

    while ((line = br2.readLine()) != null)

    Normally in while and if statements, use two "=" in a row for equals.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Lightbulb Re: need help inputting values into an array

    Wait...I see a potential Null Pointer Exception!

    You never initialize the String variable line nor do you change it to anything.

    Try changing it to

    while ((br2.readLine()) != null) {
    lineCount++;

    I know that a Null Pointer Exception is occurring because line was never initialized.

    Hence

    while ((line = br2.readLine()) != null) {
    lineCount++;

    will tell it...maybe since you have only 1 "="...to stop the while loop if line is equal to null.

    Well, it's never been initialized so it is null.

    Ok...that itself might not be a Null Pointer Exception.

    However, as it is, lineCount is now still 0.

    Therefore your array is empty and you're printing null values at the end.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: need help inputting values into an array

    One last time Javapenguin...did you even try that piece of code you are trying to advise someone else on? Continuing to hand out unsolicited, untested advice eh?

    pds8475, it helps to see the full stack trace...this contains important information. To define the problem...BufferedReader.readLine returns null when the end of stream is reached. You read until this point, then try to read another line below, thus the reader returns null. To get around not knowing how many lines there are in the file, use an ArrayList so you can collect all the lines without the need to count the lines then read the file

  5. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (January 22nd, 2011)

Similar Threads

  1. Printing boolean values from an array
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 17th, 2010, 11:11 PM
  2. Populating a 2D array with textfield values
    By drixnak in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 19th, 2010, 01:20 PM
  3. Assgining values to array indexes
    By chronoz13 in forum Collections and Generics
    Replies: 3
    Last Post: December 28th, 2009, 10:09 PM
  4. inputting of text
    By Subhasis Banerjee in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: October 30th, 2009, 12:53 PM
  5. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM