Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - How to print() output to previous line after input

Tags:

java

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?

like image 544
Jeffery Huang Avatar asked Jul 12 '17 22:07

Jeffery Huang


People also ask

How do I go back to the previous line in Java?

The trick is : \033[F is an ANSI code (which is supported by most terminals) to go back one line. Then, \r is to go back at the beginning of the line. You can rewrite it from there. You may need to pad it with white spaces to clear it correctly.

How do I print a previous line?

go to the (start of the) previous line: \033[F.

How do I save console output to string Java?

If you create a PrintStream connected to a ByteArrayOutputStream , then you can capture the output as a String . Example: // Create a stream to hold the output ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); // IMPORTANT: Save the old System. out!

How do you print output in Java?

print() - It prints string inside the quotes. println() - It prints string inside the quotes similar like print() method. Then the cursor moves to the beginning of the next line. printf() - It provides string formatting (similar to printf in C/C++ programming).


1 Answers

You should look into Ansi Escape Codes.

Something like:

public void update() {
    final String guessString = keyboard.next();

    System.out.println("\033[1A\033[" + guessString.length() + 'C'
        + "\t{next col}");
}

will result in:

1234   {next col}

(where 1234, followed by {RETURN} was entered by the user; in an appropriately supporting console.

"\033[1A" moves the cursor up 1 row; and "\033[xC" moves the cursor x to the right (where above we calculate x as the length of their guess).

Another option that you have with this approach is to clear the console ("\033[2J") so that you can just completely re-draw the table (which is probably easier to maintain).

like image 66
BeUndead Avatar answered Sep 19 '22 18:09

BeUndead