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
Printable View
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.
You could possibly do something like this. As an example, it will take in user input and print out the 10th line.
Code Java: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; } } } }
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? :)Quote:
If you were reading in a file with the Scanner class, this wouldn't be a problem.
oh ok I see now.
Thanks KevinWorkman.
No problem. So did you get it all figured out?
Glad I could help.
May be worth taking a look at - http://www.javaprogrammingforums.com...ner-class.html