Results 1 to 19 of 19
- 05-04-2010, 10:56 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 15
- Rep Power
- 0
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
- 05-05-2010, 09:29 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
You really do need to indent your code.
It makes it a lot easier to picture the flow.
That said...
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.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; } }
- 05-05-2010, 02:47 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 15
- Rep Power
- 0
tht works a treat mate, i really appreciate it, i know i need to start indenting properly.
- 05-18-2010, 07:00 PM #4
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
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;
So instead of;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"); } }
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
- 05-18-2010, 07:43 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 05-18-2010, 09:17 PM #6
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
- 05-18-2010, 09:22 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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).
- 05-18-2010, 11:26 PM #8
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
Do you mean to declare i has an int outside the while loop?
Then just have 1 = 0 in the for statementJava Code:int i;
and i++ after;Java Code:for (i=0;i<teams.length;i++)
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:Java Code:teams[i]=testTeam;
- 05-19-2010, 12:11 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
You don't need the for loop. Just have a value declared outside the while loop, and increment it each iteration.
The for loop you currently have in your code just overwrites the entire array with the current element every time the while loop executes.Java Code:int[] array = new array[10]; int i = 0; while(i < 10) { array[i] = i; i++; }Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-20-2010, 10:49 AM #10
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
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
- 05-20-2010, 11:16 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
What does your code look like now (bearing in mind the code m00nchile has given)?
- 05-20-2010, 11:31 AM #12
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
I can't put the code m00nchile gave as it gives an error that it can't find symbol class array.
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?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++; } } } }
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
- 05-20-2010, 11:41 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
m00nchile was showing you the structure, not giving you cut and paste code.
this:
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.Java Code:for(i=0;i<teams.length;) { teams[i] = testTeam; i++; }
- 05-20-2010, 11:46 AM #14
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 05-20-2010, 11:51 AM #15
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
But where do I put the while loop do I replace the while loop already in place?
I put it here;Java Code:while (bufferedScanner.hasNextLine())
it now populates the teams array with the first line in the file.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++; } } }
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 :)
- 05-20-2010, 11:53 AM #16
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 05-20-2010, 11:54 AM #17
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
I got it working :) Thanks again for your patience :D
- 05-20-2010, 05:26 PM #18
Member
- Join Date
- Apr 2010
- Posts
- 15
- Rep Power
- 0
soph your code here looks very similar :confused: im sure ive seen it before ;)
- 05-24-2010, 11:45 AM #19
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Scanner, while loop and sorting arrays/string?
By RSYR in forum New To JavaReplies: 10Last Post: 04-20-2011, 06:13 PM -
Objects and Arrays
By bannow in forum New To JavaReplies: 3Last Post: 04-19-2010, 07:15 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
[SOLVED] Arrays of Objects with Subscripts
By Sidmyre in forum New To JavaReplies: 5Last Post: 12-12-2008, 02:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks