Results 1 to 19 of 19
  1. #1
    nevermiind is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default scanner objects with arrays again..

    hey,

    i have been writing this method some time now and i think im nearly there, at top of my class i have an array as follows: private Team[] teams; the below should process all the info parsed from a text file and using methods in the "team" class to add points as necasssary taken from each token, then add the individual teams to the array teams[], all of this seems to work, however when i inspect the array teams, every entry in the object is the last line of the text file eg. Amalthea,0,0,4,0,1 when there should be 5 different entrys as seen in the text file contents at the bottom, any assitance would be apprecitated, thanks.

    public void loadTeams()
    {
    OUDialog.alert("Select input file for " + this.getGroupName());
    String pathname = OUFileChooser.getFilename();
    File aFile = new File(pathname);
    Scanner bufferedScanner = null;

    try
    {

    Scanner lineScanner;
    String currentLine;
    bufferedScanner = new Scanner(new BufferedReader(new FileReader(aFile)));
    groupName = bufferedScanner.next();
    if(groupName.equals(this.getGroupName()))
    {
    currentLine = bufferedScanner.nextLine();
    while (bufferedScanner.hasNextLine())
    {
    currentLine=bufferedScanner.nextLine();
    lineScanner = new Scanner(currentLine);
    lineScanner.useDelimiter(",");
    Team aTeam = new Team(lineScanner.next());
    aTeam.setOrigin(lineScanner.nextInt());
    aTeam.setForm(lineScanner.nextInt());
    aTeam.setBeing(lineScanner.nextInt());
    aTeam.setMoral(lineScanner.nextInt());
    aTeam.setPhilosophical(lineScanner.nextInt());
    aTeam.calculateQualifyingPoints();

    for (int i=0;i<teams.length;i++)
    {
    teams[i]=aTeam;
    }
    }

    }

    }
    catch (Exception anException)
    {
    System.out.println("Wrong File Selected" + anException);
    }
    finally
    {
    try
    {
    bufferedScanner.close();
    }
    catch (Exception anException)
    {
    System.out.println("Could not close file");
    }
    }
    }

    Text file contents:
    Group 1
    Io,3,1,1,2,0
    Europa,1,2,3,0,1
    Ganymede,4,0,0,3,0
    Callisto,2,1,2,0,1
    Amalthea,0,0,4,0,1

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    You really do need to indent your code.
    It makes it a lot easier to picture the flow.

    That said...
    Java Code:
    while (bufferedScanner.hasNextLine())
    {
        currentLine=bufferedScanner.nextLine();
        lineScanner = new Scanner(currentLine);
        lineScanner.useDelimiter(",");
        Team aTeam = new Team(lineScanner.next());
        // Snipped setup of team values.
        for (int i=0;i<teams.length;i++)
        {
            teams[i]=aTeam;
        }
    }
    That second loop is your problem. Each time you create a Team you then go and populate the entire teams array with that team. You need to have an index (i), declared outside the while loop, that you increment (i++) at the end of the while loop...just after assigning teams[i] = aTeam.

  3. #3
    nevermiind is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default

    tht works a treat mate, i really appreciate it, i know i need to start indenting properly.

  4. #4
    soph is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    Hi im having issues with the same thing I can only get it to print one line from the text file then it throws up a Exception: java.lang.NullPointerException.
    My code is as follows;

    Java Code:
    public void loadTeams()
       {
          OUDialog.alert("Select input file for " + this.getGroupName());
          String pathname = OUFileChooser.getFilename();
          File testFile = new File(pathname);
          Scanner bufferedScanner = null;
    
          try
          {
    
                Scanner lineScanner;
                String currentLine;
                bufferedScanner = new Scanner(new BufferedReader(new FileReader(testFile)));
                groupName = bufferedScanner.next();
                if(groupName.equals(this.getGroupName()))
               {
                currentLine = bufferedScanner.nextLine();
                
           while (bufferedScanner.hasNextLine())
                {
                   
                   currentLine=bufferedScanner.nextLine();
                   lineScanner = new Scanner(currentLine);
                   lineScanner.useDelimiter(",");
                   Team testTeam = new Team(lineScanner.next());
                   testTeam.setOrigin(lineScanner.nextInt());
                   testTeam.setForm(lineScanner.nextInt());
                   testTeam.setBeing(lineScanner.nextInt());
                   testTeam.setMoral(lineScanner.nextInt());
                   testTeam.setPhilosophical(lineScanner.nextInt());
                   testTeam.calculateQualifyingPoints();
                   
                  
            for (int i=0;i<teams.length;i++)
                   {
                      teams[i]=testTeam;
                      i++;
                   }
                }
    
             }
    
          }
          catch (Exception anException)
          {
             System.out.println("Wrong File Selected" + anException);
          }
          finally
          {
             try
             {
                bufferedScanner.close();
             }
             catch (Exception anException)
             {
                System.out.println("Could not close file");
             }
          }
    So instead of;
    Statistics for Group
    Io,3,1,1,2,0
    Europa,1,2,3,0,1
    Ganymede,4,0,0,3,0
    Callisto,2,1,2,0,1
    Amalthea,0,0,4,0,1

    I get;
    Statistics for Group
    Amalthea 0 0 4 0 1 0
    Exception: java.lang.NullPointerException

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    WHere's the NPE being thrown from...stack trace and point out the line.

    You're also doing the same thing the OP did, so read what I said about populating the array.

  6. #6
    soph is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    WHere's the NPE being thrown from...stack trace and point out the line.

    You're also doing the same thing the OP did, so read what I said about populating the array.
    How do I stack trace and where would I implement this?
    its the OU so they have there own java enviroment??

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    Well, the stack trace is usually the load of stuff after the name of the exception.
    When catching exceptions you ought to usually do ex.printStackTrace(), otherwise you lose the information that tells you where that exception was thrown from (ie where the exception occurred).

  8. #8
    soph is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    You need to have an index (i), declared outside the while loop, that you increment (i++) at the end of the while loop...just after assigning teams[i] = aTeam.
    Do you mean to declare i has an int outside the while loop?
    Java Code:
    int i;
    Then just have 1 = 0 in the for statement
    Java Code:
     for (i=0;i<teams.length;i++)
    and i++ after;
    Java Code:
    teams[i]=testTeam;
    I'm sorry for being so dense its been a very long few days and I can't see what's probably staring me in the face :confused:

  9. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You don't need the for loop. Just have a value declared outside the while loop, and increment it each iteration.
    Java Code:
    int[] array = new array[10];
    int i = 0;
    while(i < 10) {
      array[i] = i;
      i++;
    }
    The for loop you currently have in your code just overwrites the entire array with the current element every time the while loop executes.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    soph is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    Still can't get this tp work ??? Its driving me nuts its my deadline tomorrow so probably not going to get this done now but thanks for trying to help me :o

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    What does your code look like now (bearing in mind the code m00nchile has given)?

  12. #12
    soph is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    I can't put the code m00nchile gave as it gives an error that it can't find symbol class array.

    Java Code:
     public void loadTeams()
       {
          OUDialog.alert("Select input file for " + this.getGroupName());
          String pathname = OUFileChooser.getFilename();
          File testFile = new File(pathname);
          Scanner bufferedScanner = null;
          int i;
          try
          {
             
                Scanner lineScanner;
                String currentLine;
                bufferedScanner = new Scanner(new BufferedReader(new FileReader(testFile)));
                groupName = bufferedScanner.next();
                if(groupName.equals(this.getGroupName()))
               {
                currentLine = bufferedScanner.nextLine();
                            
                while (bufferedScanner.hasNextLine())
    
                {
                   
                   currentLine=bufferedScanner.nextLine();
                   lineScanner = new Scanner(currentLine);
                   lineScanner.useDelimiter(",");
                   Team testTeam = new Team(lineScanner.next());
                   testTeam.setOrigin(lineScanner.nextInt());
                   testTeam.setForm(lineScanner.nextInt());
                   testTeam.setBeing(lineScanner.nextInt());
                   testTeam.setMoral(lineScanner.nextInt());
                   testTeam.setPhilosophical(lineScanner.nextInt());
                   testTeam.calculateQualifyingPoints();
                   
                   for(i=0;i<teams.length;)
                   {
                   teams[i] = testTeam;
                   i++;
                }
                  
             }
             }
             
          }
    I tried to use the advice in your post to the OP but its still filling the teams array with the 1 team? It fills it with the last team in the file?

    Statistics for Group
    Amalthea 0 0 4 0 1 0
    Amalthea 0 0 4 0 1 0
    Amalthea 0 0 4 0 1 0
    Amalthea 0 0 4 0 1 0
    Amalthea 0 0 4 0 1 0

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    m00nchile was showing you the structure, not giving you cut and paste code.

    this:
    Java Code:
                   for(i=0;i<teams.length;)
                   {
                       teams[i] = testTeam;
                       i++;
                   }
    is wrong. This is where you are filling the whole teams array with a single team, everytime you go round the while loop. You just want to fill in one slot in the teams array with the new team.

  14. #14
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    And your error with the for loop was stated many times in this thread. Look, go through your code slowly and carefully. Take a piece of paper and a pencil, and work out what is happenning, because your "code not working" comment shows that you pretty much want someone to write everything for you, and that's not what this forum is about.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  15. #15
    soph is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    But where do I put the while loop do I replace the while loop already in place?
    Java Code:
    while (bufferedScanner.hasNextLine())
    I put it here;
    Java Code:
     public void loadTeams()
       {
          OUDialog.alert("Select input file for " + this.getGroupName());
          String pathname = OUFileChooser.getFilename();
          File testFile = new File(pathname);
          Scanner bufferedScanner = null;
          int i = 0;
          try
          {
             
                Scanner lineScanner;
                String currentLine;
                bufferedScanner = new Scanner(new BufferedReader(new FileReader(testFile)));
                groupName = bufferedScanner.next();
                if(groupName.equals(this.getGroupName()))
               {
                currentLine = bufferedScanner.nextLine();
                            
                while (bufferedScanner.hasNextLine())
    
                {
                   
                   currentLine=bufferedScanner.nextLine();
                   lineScanner = new Scanner(currentLine);
                   lineScanner.useDelimiter(",");
                   Team testTeam = new Team(lineScanner.next());
                   testTeam.setOrigin(lineScanner.nextInt());
                   testTeam.setForm(lineScanner.nextInt());
                   testTeam.setBeing(lineScanner.nextInt());
                   testTeam.setMoral(lineScanner.nextInt());
                   testTeam.setPhilosophical(lineScanner.nextInt());
                   testTeam.calculateQualifyingPoints();
                   
                   while(i < teams.length)
                   {
                   teams[i] = testTeam;
                   i++;
                }
                  
             }
             }
    it now populates the teams array with the first line in the file.
    Statistics for Group
    Io 3 1 1 2 0 21
    Io 3 1 1 2 0 21
    Io 3 1 1 2 0 21
    Io 3 1 1 2 0 21
    Io 3 1 1 2 0 21

    Thanks for you patience :)

  16. #16
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Declare the variable outside of the while(bufferedScanner.hasNextLine()) loop. You don't need a loop inside of a loop, as has been stated multiple times already.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  17. #17
    soph is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    I got it working :) Thanks again for your patience :D

  18. #18
    nevermiind is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default

    soph your code here looks very similar :confused: im sure ive seen it before ;)

  19. #19
    soph is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    All I can say is thanks for helping me without even realising ;)
    I did have the majority of the code written before I used the power of google though lol
    Seriously though thank god that was the last one its seriously stressed me out this year :mad:

Similar Threads

  1. Scanner, while loop and sorting arrays/string?
    By RSYR in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 06:13 PM
  2. Objects and Arrays
    By bannow in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 07:15 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  5. [SOLVED] Arrays of Objects with Subscripts
    By Sidmyre in forum New To Java
    Replies: 5
    Last Post: 12-12-2008, 02:18 AM

Posting Permissions

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