I a newbie to java so please don't rate down if this sounds absolute dumb to you
ok how do I enter this using a single scanner object
5
hello how do you do
welcome to my world
6 7
for those of you who suggest
scannerobj.nextInt->nextLine->nextLine->nextInt->nextInt,,,
check it out, it does not work!!!
thanks
Using Scanner. It splits the string into tokens by whitespace delimiter. The complete token is identified by the input that matches the delimiter pattern.
The nextLine() method of java. util. Scanner class advances this scanner past the current line and returns the input that was skipped. This function prints the rest of the current line, leaving out the line separator at the end.
You can get multiple user inputs from the same instance of Scanner. Here is an example: Scanner input = new Scanner(System.in); int x = input. nextInt(); int y = input.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please specify how many lines you want to enter: ");
String[] input = new String[in.nextInt()];
in.nextLine(); //consuming the <enter> from input above
for (int i = 0; i < input.length; i++) {
input[i] = in.nextLine();
}
System.out.printf("\nYour input:\n");
for (String s : input) {
System.out.println(s);
}
}
Sample execution:
Please specify how many lines you want to enter: 3
Line1
Line2
Line3
Your input:
Line1
Line2
Line3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With