1

I'm trying to make something that looks like this:

Move # to #. For example, Move 1 to 3

I tried System.out.print("move " + input.nextInt() + " to " + input.nextInt())

This works fine, but I don't know how to assign values from those inputs.

This works, but it's NOT what I want:

System.out.print("move ");
int first = input.nextInt();
System.out.print(" to ");
int second = input.nextInt();

I don't want this, because instead of move # to #, it gives this:

move #
to #

How can I keep all inputs in a single line while still assigning the input values to variables?

In order, I want the line to look like this:

before first input:

Move _

after first input:

Move # to _

after second input:

Move # to #
Mark Puchala II
  • 634
  • 2
  • 8
  • 25
  • @shmosel I want `to` to appear after the first input, but before the second input. So simply pressing space would do nothing. – Mark Puchala II Feb 12 '17 at 06:08
  • I take that back. But your question is confusing. The first example implies you want to input both values before printing them out. The second implies you want to input them inline with your output. Which is it? – shmosel Feb 12 '17 at 06:21
  • @shmosel I edited the second example to re-state that it's what I DON'T want. I want the first, not the second. – Mark Puchala II Feb 12 '17 at 07:10
  • The implication was that the only issue in the second example is the newline. If you don't mind inputting and then printing, just do `System.out.print("move " + first + " to " + second);`. – shmosel Feb 12 '17 at 07:12
  • @shmosel Finding a way to print while inputting is actually the whole point of the question. – Mark Puchala II Feb 12 '17 at 07:14
  • Now you're going back to the second way... Anyway, I think I understand now. Unfortunately I suspect there's no easy way to do what you want. – shmosel Feb 12 '17 at 07:16
  • Note that a computer program rarely does two things "simultaneously", especially at a beginner level. This is probably just a poor choice of words, but I wanted to point it out for clarification. – Code-Apprentice Feb 12 '17 at 07:17
  • @shmosel Are you suggesting it's impossible to gain the stored values illustrated in the second way while also keeping things to a single line? – Mark Puchala II Feb 12 '17 at 07:17
  • @shmosel Thank you for all your help with refining my questions, by the way! I actually have a bad habit of wording things in a confusing manner. – Mark Puchala II Feb 12 '17 at 07:17
  • @Code-Apprentice I assume it's a poor choice of words. In a way... I guess I'm just asking how I can execute something like `int variable = Scanner.nextInt()` without it adding a new line. – Mark Puchala II Feb 12 '17 at 07:20
  • I think you'll need something like this: [How to read a single char from the console in Java (as the user types it)?](http://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it) – shmosel Feb 12 '17 at 07:20

1 Answers1

0

The problem is that Scanner reads input until the user presses Enter which inserts a newline character into the buffer. This character will be printed and cause the next output to appear on the next line. I don't think you can do what you want with Scanner. You will need to use a InputStream or Reader class instead.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • How can `InputStream` or `Reader` run something equivalent to `int variable = Scanner.nextInt()` without a newline? – Mark Puchala II Feb 12 '17 at 07:21
  • @MarkPuchalaII With a lot more work on your part. One way is to read each character one at a time yourself and then convert the input into a number. Since you read each character, you can use something other than the newline, such as a space, as a delimiter. – Code-Apprentice Feb 12 '17 at 07:26