Results 1 to 4 of 4
  1. #1
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default help inputting values into an array

    hi im trying to enter the values from gn.txt into the array guestNumbers
    but when im running the program im getting a null pointer exception
    Java Code:
     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;
    }
    
    System.out.println(guestNumbers[0]);
    System.out.println(guestNumbers[1]);
    System.out.println(guestNumbers[2]);
    System.out.println(guestNumbers[3]);

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi, Can you show content a file "gn.text"?
    What number line, where you have exception?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    Quantum is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default

    The FileReader constructor expects a File. Create the File first.

  4. #4
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    right iv now got it to ouput to the screen the right values using the code
    Java Code:
        String e1;
           double e;
           for(int i=0; ng.hasNext();i++){
             e1=ng.next();
             e=Double.parseDouble(e1);
             guestNumbers[i]=e;
           }
    
    
    System.out.println(guestNumbers[0]);
    System.out.println(guestNumbers[1]);
    System.out.println(guestNumbers[2]);
    System.out.println(guestNumbers[3]);
    this outputs to the screen as:

    1.0
    2.0
    3.0
    1.0

    now i need to add all the values together
    any idea's

Similar Threads

  1. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  2. Pass Values into 2D Array?
    By noble in forum New To Java
    Replies: 7
    Last Post: 11-09-2010, 07:30 AM
  3. An Array of different integer values
    By lithium002 in forum New To Java
    Replies: 7
    Last Post: 12-04-2009, 05:25 AM
  4. Same values in an array
    By hawaiifiver in forum New To Java
    Replies: 3
    Last Post: 02-24-2009, 08:33 PM
  5. replacing array values
    By Jononomous in forum New To Java
    Replies: 1
    Last Post: 05-22-2008, 03:27 PM

Posting Permissions

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