0

As a training exercise, I have to take user input for a guess they will make, which consists of 4 integers. So far, I've written this:

// IV. The computer asks the user to guess the 4-tuples. The user input must
// be in the same line as the question, a space between each two numbers, and 
// an enter after the last one (as shown in the example).

System.out.print("Iteration 1. Next guess: ");
int Guess1 = x.nextInt();
System.out.print(" ");
int Guess2 = x.nextInt();
System.out.print(" ");
int Guess3 = x.nextInt();
System.out.print(" ");
int Guess4 = x.nextInt();
System.out.println(" ");

The output is receive, however, is as follows:

Iteration 1. Next guess: 1    
2    
2   
2

Even though I'm using the "print" function instead of "println" function, it seems to go to next line when an input is given by the user. Any ideas to make sure the input numbers show up in the console on the same line? So like this:

Iteration 1. Next guess: 1 2 2 2
3
  • Just don't press enter after you input each number. Commented Feb 4, 2018 at 15:04
  • But how can I make it foolproof, meaning that it automatically formats it for the user? Commented Feb 4, 2018 at 15:07
  • If the user presses enter whilst entering your numbers, there's not much you can do about it. Writing "Next guess (4 numbers without pressing enter)" as the instruction would help. Commented Feb 4, 2018 at 15:09

2 Answers 2

4

Enter your input with space:

System.out.print("Iteration 1. Next guess: ");
  int Guess1 = x.nextInt();
  int Guess2 = x.nextInt();
  int Guess3 = x.nextInt();
  int Guess4 = x.nextInt();
Sign up to request clarification or add additional context in comments.

Comments

1

don't worry about user input formate for now, in an advanced training you will use GUI to interact with the user.

for now. add your input in a single line separated by space. the system will recognize these numbers and ignore space. after you add the four numbers press enter. remove System.out.print("")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.