Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reset scanner to beginning of file

796468Sep 18 2008 — edited Sep 18 2008
I have a method to check if an account number is present within a list of numbers in a file. It can be executed multiple times. However, I need to read the file from the start each time, so the scanner needs to be reset somehow. Is this the best way to do it?
.    
    public static boolean verifyAccount(int accountNum)
    {
        boolean valid = false; 
        while (file.hasNextInt())
            if (accountNum == file.nextInt()) 
                valid = true;                
        
        /*Reset Scanner to beginning of file*/
        file.close();
        try
        {  
            file = new Scanner(new File("VALID ACCOUNTS.TXT"));   
        }//end try
        catch (FileNotFoundException e)
        {
            System.err.println("ERROR: VALID ACCOUNTS.TXT could not be found");
        }//end catch   
        return valid;
    }//end verifyAccount
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 16 2008
Added on Sep 18 2008
3 comments
3,277 views