Results 1 to 9 of 9
Thread: Buffered Reader and scanners
- 05-04-2010, 04:42 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 15
- Rep Power
- 0
Buffered Reader and scanners
Hey peeps, ive previously posted on this topic but am pulling my hair out now, in the below code i read in a text file, contents at the bottom of the page and if the top line matches the name of the file, eg group 1.txt is found in that text file then continue looping through the file and then i have to use the other data in there to create an object with specific properties, the problem im having is when im trying to simply print out the tokens in the file so i can see its working correctly i get a java.util.NoSuchElementException, like theres no more data there even thou there is, any thoughts?
The text file contents: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)));
currentLine = bufferedScanner.nextLine();
lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
groupName = lineScanner.next();
if(groupName.equals(this.getGroupName()))
{
while (bufferedScanner.hasNextLine())
{
System.out.println(lineScanner.next());
currentLine=bufferedScanner.nextLine();
}
}
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
finally
{
try
{
bufferedScanner.close();
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
}
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,1Last edited by nevermiind; 05-04-2010 at 05:23 PM.
- 05-04-2010, 05:26 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
You need to indent your code.
That aside, why are you using a Scanner wrapped round the BufferedReader? Do you actually gain anything from doing that? I'm not even sure you need a scanner at all. Just readLine(), and split().
- 05-04-2010, 05:30 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 15
- Rep Power
- 0
ye i have to use two scanner objects for full marks on this one, i could use readLine() but i shouldnt, im not sure if i have to initliase the second scanner in the while loop?
- 05-04-2010, 05:36 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
It's assignments like this that make me tear my hair out. Why force you to use two Scanners? Utterly pointless.
I can't actually think of a piece of code I've written that's even used a Scanner.
Anyway, silly restrictions aside, which line is throwing the exception. If you don't know which line, then you need to do anException.printStackTrace() in your catch blocks, rather than simply printing the name of the exception.
- 05-04-2010, 05:39 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 15
- Rep Power
- 0
System.out.println(lineScanner.next()); is causing the problem, almost as if no other elements exsist in the text file, but if i do a System.our.println(currentLine); i can see all of the files info like i would expect to...
- 05-04-2010, 05:48 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
Ah, I can see it:
Now, it's the bit in bold on your first pass through. You are getting the next element of the line:Java Code:lineScanner = new Scanner(currentLine); // Get the scanner lineScanner.useDelimiter(","); // split on comma groupName = lineScanner.next(); // get the first element if(groupName.equals(this.getGroupName())) { while (bufferedScanner.hasNextLine()) // Do we have another line...not we're not getting it, though. { System.out.println(lineScanner.next()); // [B]Get the second element[/B] currentLine=bufferedScanner.nextLine(); // get the new line. } }
Group 1
and there is no next element.
- 05-04-2010, 05:52 PM #7
Member
- Join Date
- Apr 2010
- Posts
- 15
- Rep Power
- 0
so do i need to stick a nextLine() in ther i guess so it jumps to the next avilable line?
- 05-04-2010, 05:56 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
The order up to the loop is OK, but then you need to get the next line and process it (ie get a new Scanner for that line)...then you can loop over that Scanner.
- 05-04-2010, 06:22 PM #9
Member
- Join Date
- Apr 2010
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Buffered Reader
By ilovepolluting in forum New To JavaReplies: 2Last Post: 02-04-2010, 09:16 AM -
Buffered Reader problem
By pradeep.theonlyone in forum New To JavaReplies: 3Last Post: 07-31-2009, 11:37 AM -
Buffered Reader Exception
By hitmen in forum New To JavaReplies: 6Last Post: 01-07-2009, 11:14 AM -
FileReader / Buffered Reader
By sepaht in forum New To JavaReplies: 9Last Post: 07-10-2008, 08:05 PM -
Scanners / Printers
By cytech in forum New To JavaReplies: 0Last Post: 03-17-2008, 05:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks