Results 1 to 8 of 8
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default How do I reset my Scanner object ?

    Java Code:
    		Scanner myScanner = new Scanner(new FileInputStream("c:/try.txt"));
    		
    		int numberOfLines = 0;
    		int total = 0;
    		
    		while(myScanner.hasNextLine()) 
    		{
    			myScanner.nextLine();
    			numberOfLines++;
    		}
    		
    		myScanner.reset();
    		
    		for(int i = 0; i<numberOfLines; i++)
    		{
    			if(myScanner.hasNextInt())
    			{
    				total = total + myScanner.nextInt();
    			}
    			else
    			{
    				System.out.println("I am in else!");
    			}
    		}
    		
    		System.out.println();
    		System.out.println("Number of lines: " + total);
    I have a file called try.txt and it has 3 lines of text in it:
    1
    1
    1

    What I am trying to do is,
    get the number of the lines in it,
    See if the nextline is an integer,
    And if so, add it to sum.

    But my problem is,
    Since my scanner scans thru the file, it is already in the end of the file.

    How can I get my Scanner object to the beginning of the file again?
    Java Code:
    myScanner.reset();
    doesn't seem to do anything ?

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How do I reset my Scanner object ?

    BTW
    my code results in:

    I am in else!
    I am in else!
    I am in else!

    Number of lines: 0

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,269
    Rep Power
    25

    Default Re: How do I reset my Scanner object ?

    Why not read in all the lines from the file and process them in memory instead of reading and rereading and rereading the lines.
    Reading from a disk is much more expensive than looking through Strings in memory.

    If the file is really hugh, then you might consider another way.

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How do I reset my Scanner object ?

    I really want to reset my Scanner object.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,269
    Rep Power
    25

    Default Re: How do I reset my Scanner object ?

    What methods are available in the Scanner class? Do any of them do what you want?
    If there are none then there are none.
    If you want to "rewind" the location where you are reading in a file then you will have to use a different class to do the reading.

    You can always create a new instance of the Scanner class.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How do I reset my Scanner object ?

    As far as I know, you can't. But why bother? Why not simply read through the file with one while loop?

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How do I reset my Scanner object ?

    Thanks.

    Creating a new Object works.

    I guess there is no reseting then.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 6
    Last Post: 05-09-2013, 08:51 AM
  2. Java, Reset Visibility Of An Object
    By Alerhau in forum New To Java
    Replies: 1
    Last Post: 08-10-2011, 11:53 PM
  3. Reset a Scanner to the beginning of a file
    By Nosrettap in forum New To Java
    Replies: 2
    Last Post: 02-06-2011, 03:23 AM
  4. Replies: 3
    Last Post: 09-24-2008, 11:59 PM
  5. Replies: 1
    Last Post: 12-21-2007, 11:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •