0

I am trying to use a user-entered number from the scanner in my code in multiple places. Below the line should be the same user-entered number as the first. Is this possible?

Scanner inputHere = new Scanner(system.in);
System.out.println("Please select me");
String inputHereOne = inputHere.nextLine();
System.out.println("----------------------------------");
user9424843
  • 33
  • 1
  • 5

1 Answers1

1

Yes, it's possible. Use the value stored in inputHereOne. No need to call inputHere.nextLine() until you actually need the next line of input. Like so:

System.out.println(inputHereOne);
System.out.println(inputHereOne);

This will print inputHereOne, twice.

Avi
  • 2,359
  • 1
  • 9
  • 21
  • @user9424843 That code will (line 1) create a Scanner reading from System input, (2) print "Please select me", (3) create a String variable `inputHereOne` with value equals to the next line from System input, and (4) print lots of dashes. After that code has run, you can use the value read into `inputHereOne` as you would like, by simply referring to `inputHereOne`. For example, to parse the `int` value out of `inputHereOne`, you might say something like `int inputValue = Integer.parseInt(inputHereOne);`. From then on, you could refer to the value as an int by referring to `inputValue`. – Avi Sep 22 '19 at 01:17
  • @user9424843 Please do some research. There are plenty of resources available on the [site](https://stackoverflow.com/a/2506146/5699679) and Google to figure out how to do simple input reading. – Avi Sep 22 '19 at 01:22