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

    Default 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?

    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);
    }
    }
    }
    The 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
    Last edited by nevermiind; 05-04-2010 at 05:23 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    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().

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

    Default

    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?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    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.

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

    Default

    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...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Ah, I can see it:
    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.
        }
    }
    Now, it's the bit in bold on your first pass through. You are getting the next element of the line:
    Group 1
    and there is no next element.

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

    Default

    so do i need to stick a nextLine() in ther i guess so it jumps to the next avilable line?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    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.

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

Similar Threads

  1. Buffered Reader
    By ilovepolluting in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 09:16 AM
  2. Buffered Reader problem
    By pradeep.theonlyone in forum New To Java
    Replies: 3
    Last Post: 07-31-2009, 11:37 AM
  3. Buffered Reader Exception
    By hitmen in forum New To Java
    Replies: 6
    Last Post: 01-07-2009, 11:14 AM
  4. FileReader / Buffered Reader
    By sepaht in forum New To Java
    Replies: 9
    Last Post: 07-10-2008, 08:05 PM
  5. Scanners / Printers
    By cytech in forum New To Java
    Replies: 0
    Last Post: 03-17-2008, 05:54 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
  •