Results 1 to 8 of 8
- 01-13-2012, 08:38 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
How do I reset my Scanner object ?
I have a file called try.txt and it has 3 lines of text in it: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);
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?
doesn't seem to do anything ?Java Code:myScanner.reset();
- 01-13-2012, 08:38 PM #2
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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
- 01-13-2012, 08:56 PM #3
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.
- 01-13-2012, 08:57 PM #4
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: How do I reset my Scanner object ?
I really want to reset my Scanner object.
- 01-13-2012, 09:04 PM #5
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.
-
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?
- 01-13-2012, 09:05 PM #7
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: How do I reset my Scanner object ?
Thanks.
Creating a new Object works.
I guess there is no reseting then.
-
Re: How do I reset my Scanner object ?
Again, no need to create a new object, no need to reset. Just read through once with a while loop.
Similar Threads
-
How do I scan a photo from my scanner into an ImageIcon object
By fortwnty420 in forum Advanced JavaReplies: 6Last Post: 05-09-2013, 08:51 AM -
Java, Reset Visibility Of An Object
By Alerhau in forum New To JavaReplies: 1Last Post: 08-10-2011, 11:53 PM -
Reset a Scanner to the beginning of a file
By Nosrettap in forum New To JavaReplies: 2Last Post: 02-06-2011, 03:23 AM -
Why do I need to declare a new Scanner object here?
By Chase in forum New To JavaReplies: 3Last Post: 09-24-2008, 11:59 PM -
Regarding Scanner Object -- Cannot Resolve the Symbol
By pascal45 in forum New To JavaReplies: 1Last Post: 12-21-2007, 11:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks