Hi guys
Can Scanner read the specified line from user's input?
For example, I want Scanner to find the 50th line of input and read it only, not from the first line, how to do that?
Many Thanks
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi guys
Can Scanner read the specified line from user's input?
For example, I want Scanner to find the 50th line of input and read it only, not from the first line, how to do that?
Many Thanks
Hello ice,
The problem with the Scanner is, as soon as the user presses enter, it takes that as the input.
There isn't a way to move to the next line without pressing enter.
This is assuming you are taking user input from the console though.
If you were reading in a file with the Scanner class, this wouldn't be a problem.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
You could possibly do something like this. As an example, it will take in user input and print out the 10th line.
import java.util.Scanner; public class ScannerTest { /** * JavaProgrammingForums.com */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter something:"); int count = 1; while (sc.hasNext()) { String ignore = sc.nextLine(); count++; if (count == 10) { String keep = sc.nextLine(); System.out.println("This is the 10th line: " + keep); break; } } } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
ice (January 27th, 2011)
Thank you so much JavaPF!! ..especially the example you provided.
, you mean Scanner can read the specified line from a file? If so, then how to do that?If you were reading in a file with the Scanner class, this wouldn't be a problem.
Last edited by ice; January 26th, 2011 at 05:59 PM.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
oh ok I see now.
Thanks KevinWorkman.
No problem. So did you get it all figured out?
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Glad I could help.
May be worth taking a look at - http://www.javaprogrammingforums.com...ner-class.html
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.