Results 1 to 3 of 3
- 02-06-2011, 01:44 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 13
- Rep Power
- 0
-
I think that you will need to create a new Scanner object.
- 02-06-2011, 04:23 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
I agree completely.
@OP - this is probably worth ignoring
But, strangely, the following does ... something
Java Code:import java.io.IOException; import java.io.StringReader; import java.util.Scanner; public class ScannerEg { public static void main(String[] args) throws IOException { String data = "This is a\nthree line\nimitation file"; StringReader in = new StringReader(data); Scanner scanner = new Scanner(in); int counter = 1; //while(scanner.hasNext()) { // System.out.printf("%d: %s%n", counter++, scanner.next()); //} //System.out.printf("%d: %s%n", counter++, scanner.next()); scanner.next(); in.reset(); while(scanner.hasNext()) { System.out.printf("%d: %s%n", counter++, scanner.next()); } } }
I think the scanner "reads ahead", so resetting the underlying stream will not do what you expect. It's no good skip()ping the unwanted input because once the scanner has seen the end of the stream hasNext() returns false even after the stream has been reset.
StreamTokenizer, however, is a well behaved "wrapper" that respects the capacities of the underlying stream.Last edited by pbrockway2; 02-06-2011 at 04:39 AM.
Similar Threads
-
Getting code to run from the beginning in a jFrame form
By Foxtrot in forum NetBeansReplies: 6Last Post: 01-15-2012, 05:01 AM -
Simple question Hopefully, Scanner/file
By drucey in forum New To JavaReplies: 23Last Post: 10-25-2010, 02:55 PM -
Regular expression to find strings beginning with a particular digit
By neha_sinha in forum AWT / SwingReplies: 9Last Post: 07-17-2010, 02:21 PM -
Beginning Programming with Java For Dummies
By jon01 in forum New To JavaReplies: 4Last Post: 02-16-2009, 09:40 PM -
Beginning Game Development - Suggestions?
By JDCAce in forum Advanced JavaReplies: 1Last Post: 12-08-2008, 06:42 PM
Bookmarks