3

I've made a program that asks the user for a number and then checks if it is equal, less or greater than a random number. A siple guessing game. It works perfectly but I would like to display the output on the same line as the input but I can't figure out how to do this.

Ok, I've picked a number between 1 and 100. What is your first guess?
Guess 1: 36
<
Guess 2: 50
>
Guess 3: 46
That is correct. The hidden number is 46.

This is how I want it to look like:

Guess 1: 36 <
Guess 2: 50 >
Guess 3: 46 That is correct. The hidden number is 46.

How do I do this? The cursor is moving down after the input when I use Scanner.nextInt();

This is my current code:

while (!isCorrect) {
        System.out.print("Guess " + guessCount + ":  ");
        currentGuess = input.nextInt();

        if (currentGuess == randomNumber) {
            isCorrect = true;
            System.out.println("That is correct. The hidden number was " + randomNumber);
        }
        else if (currentGuess > randomNumber) {
            System.out.println(" >");
            ++guessCount;
        }
        else if (currentGuess < randomNumber) {
            System.out.println(" <");
            ++guessCount;
        }

    }

3 Answers3

0

That's not consistently possible. You could try echoing a \b backspace character, but that probably wouldn't work. Once the user types the new line, there is no way to take it off.

tbodt
  • 16,609
  • 6
  • 58
  • 83
  • It didn't work but thank you for confirming what I was afraid of :). I guess I'll have to find a way around it somehow. –  Aug 12 '13 at 21:46
0

The best you are going to be able to accomplish is to re-echo the input.

while (!isCorrect) {
    System.out.print("Guess " + guessCount + ":  ");
    currentGuess = input.nextInt();

    if (currentGuess == randomNumber) {
        isCorrect = true;
        System.out.println("That is correct. The hidden number was " + randomNumber);
    }
    else if (currentGuess > randomNumber) {
        System.out.println(currentGuess  + " >");
        ++guessCount;
    }
    else if (currentGuess &lt randomNumber) {
        System.out.println(currentGuess  + " &lt");
        ++guessCount;
    }

}

for the output:

Guess 1: 36 
36 &lt
Guess 2: 50
50 &gt
Guess 3: 46 
That is correct. The hidden number is 46.
Chris Bode
  • 1,265
  • 7
  • 16
0

If you really need to format your output like that you could try saving your input to an arraylist, clearing the screen, then printing all the previous inputs to the screen again.

List<Integer> foo = new ArrayList<Integer>();

while (!isCorrect) {
    System.out.print("Guess " + guessCount + ": ");
    guessCount++;
    currentGuess = input.nextInt();
    foo.add(currentGuess);
    for(int a = 0; a < 50; a++) {
        System.out.println('\n');
    }
    for(int i = 0; i < foo.size(); i++) {
        currentGuess = foo.get(i);
        System.out.print("Guess " + (i+1) + ": " + currentGuess);
        if (currentGuess == randomNumber) {
            isCorrect = true;
            System.out.println(" That is correct. The hidden number was " + randomNumber);
        }
        else if (currentGuess > randomNumber) {
            System.out.println(" >");
            }
        else if (currentGuess < randomNumber) {
            System.out.println(" <");
        }
    }
}

To clear the screen I just printed out 50 blank lines.

A solution to clear the console is proposed here: Java: Clear the console however, it didn't work for me or Chris.

Community
  • 1
  • 1
Eric Lee
  • 61
  • 7
  • Runtime.getRuntime().exec("cls"); doesn't work on all windows systems. My Windows 7 x64 just ignores it. – Chris Bode Aug 12 '13 at 23:51
  • You're right, I tried it on Windows 8 x64 as well and it threw some errors. I found it here http://stackoverflow.com/questions/2979383/java-clear-the-console – Eric Lee Aug 13 '13 at 00:07
  • This seems like the best solution on my problem. Cleaning the console didn't work for me either. –  Aug 13 '13 at 06:43