I'm kind of new to Java and I'm still trying to get the hang of it, so I apologize if this is a stupid question, but I was wondering how to print the output on the same line as the input. For example, I'm programming a simple game where the user tries to guess a 4-digit number (rather like Mastermind). I've got the mechanics all figured out, but I'm having a hard time displaying it on the console. In the beginning it might look something like: (a)
Turn Guess Bulls Cows
----------------------------
1
Then, the user would input their guess: (b)
Turn Guess Bulls Cows
----------------------------
1 1234
And as soon as the user hits enter, the program should check their guess against the secret number and output the number of "bulls" (digits in their guess that match the secret number exactly) and "cows" (digits in their guess that are part of the number but in the wrong position), and then start a new line and again await user input. So if the secret number were, say, 4321... (c)
Turn Guess Bulls Cows
----------------------------
1 1234 0 4
2
Trouble is, I can't for the life of me figure out how to get the output to display on the same line as the input. Here's a snippet of what I have so far (stripped-down because the full code is much uglier):
number = "4321";
String guess;
int attempts = 1;
do
{
System.out.print(attempts + "\t\t");
guess = keyboard.next();
attempts++;
int bulls = checkBulls(guess, number);
int cows = checkCows(guess, number);
System.out.print("\t\t" + bulls + "\t\t" + cows + "\n");
}
while (!guess.equals(number));
Which gets me as far as (b), but then when I hit enter, this happens:
Turn Guess Bulls Cows
----------------------------
1 1234 // so far so good
0 4 // ack! These should've been on the previous line!
2
I know this isn't really essential to the game and is probably just me making things more complicated than necessary, but it's driving me nuts. I suppose what's happening is that when you hit enter after typing in your guess, the program starts a new line and then prints the bulls and cows. Is there any way to get around this?