开发者

Problem with buffered File Reader

I have been asked to write a file reader method, I have done one which works fine but cant get the 2nd one to work and keep getting this error after i open the booms.txt file

Error:java.util.NoSuchElementException

public instance variable

public List booms;

Code I'm using for the file reader

try

    {
        int x;
        int y;
        double boomTime;
        boolean isAHit;
        Scanner lineScanner;
        bufferedFileReader = new BufferedReader(new FileReader(aFile));
        String currentLine = bufferedFileReader.readLine();
        while (currentLine != null)
        {
            lineScanner = new Scanner(currentLine);
           开发者_如何学Python lineScanner.useDelimiter(",");
            x = lineScanner.nextInt();
            y = lineScanner.nextInt();
            boomTime = lineScanner.nextDouble();
            isAHit = lineScanner.nextBoolean();
            booms.add(new Boom(x,y,boomTime));
            currentLine = bufferedFileReader.readLine();
        }
    }
    catch (Exception anException)
    {
        System.out.println("Error:"+anException);
    }
    finally
    {
        try
        {
            bufferedFileReader.close();
        }
        catch (Exception anException)
        {
            System.out.println("Error:" +anException);
        }

}

}

Please Help


Perhaps a blank line at the end of the file?


as maurice says, probably a blank or incorrect line of data in the file.

the exception (although you haven't posted the stack trace) is being thrown by the Scanner.next... calls.

Scanner.nextInt

NoSuchElementException - if input is exhausted

this would be far more obvious to you and the members of this forum if you'd included the stack trace. try putting anException.printStackTrace(); in your catch blocks next time.


I'm using a another method that's not that complicated. just do this:

Scanner diskScanner = new Scanner(new File("pathname"));

to read for example a Integer from the specified file just type

int blahblahblah = diskScanner.nextInt();


Got it sorted I had a boolean variable which wasn't in the file it was reading :), its all about taking a break and looking at it again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜