0

So I'm trying to make a game where a user enters 5 words, then the console is cleared before his opponent then has to guess the 5 words. I've not done anything like removing spaces or converting to a consistent case because this is only me trying to learn and I already know how to do that. The program executes perfectly despite getting this long list of errors when the system tries (and fails) to clear the console. Here's the output (note: I've also used "cls" as well as "clear" and both don't work):

| Player One Enter Words | 
Word 1: why
Word 2: does
Word 3: this
Word 4: not
Word 5: work
java.io.IOException: Cannot run program "clear": CreateProcess error=2, The system   cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at OrigClass.main(OrigClass.java:18)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
| Player Two Guess Words | 
Guess 1: not
Guess 2: work
Guess 3: why
Guess 4: does
Guess 5: this
Congrats. You Got A Perfect Score Of 5

And here's my actual code:

import java.util.Arrays;
import java.util.Scanner;

class OrigClass{
    public static void main(String[] args){
    Scanner scanObj = new Scanner(System.in);
    String[] words;
    words = new String[5];
    String[] guess;
    guess = new String[5];
    System.out.println(" | Player One Enter Words | ");

    for(int count = 1; count <= 5; count++){
        System.out.print("Word " + count + ": ");
        words[count-1] = scanObj.nextLine();
    }
    try{
    Runtime.getRuntime().exec("clear");
    }
    catch (Exception e){
        e.printStackTrace();
    }
    System.out.println(" | Player Two Guess Words | ");

    for(int count2 = 1; count2 <= 5; count2++){
        System.out.print("Guess " + count2 + ": ");
        guess[count2-1] = scanObj.nextLine();
    }

    int score = 0;

    for(int count3 = 0; count3 < 5; count3++){
        if(Arrays.asList(words).contains(guess[count3])){
            score++;
        }
    }

    if(score >= 0 && score <= 2){
        System.out.println("Unlucky. You Scored " + score);
    }
    else if (score > 2 && score < 5){
        System.out.println("Good Effort. You Scored " + score);
    }
    else {
        System.out.println("Congrats. You Got A Perfect Score Of " + score);
    }
}

}

If anyone knows how to prevent this error then I'd appreciate it a lot. If you have an alternative way to clear the console, that's great, but I'd like to know why this isn't working.

Thanks

braX
  • 11,506
  • 5
  • 20
  • 33
Sam
  • 97
  • 2
  • 4
  • 11
  • possible duplicate of [Java: Clear the console](http://stackoverflow.com/questions/2979383/java-clear-the-console) – assylias Jul 17 '13 at 10:07
  • `Runtime.getRuntime().exec("clear");` tries to run a program called `clear` - it can't find it and gives you an exception. – assylias Jul 17 '13 at 10:07

2 Answers2

0

There's pretty much nothing in the console related API to do a clear screen. But, I think you can achieve the same effect through println()s. A lot of putty clients clear the page like that and then scroll up.

private static final int PAGE_SIZE = 25;

public static void main(String[] args) {
            // ...
    clearScreen();
}

private static void clearScreen() {
    for (int i = 0; i < PAGE_SIZE; i++) {
        System.out.println();
    }
}
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0
    final String ANSI_CLS = "\u001b[2J";
    final String ANSI_HOME = "\u001b[H";
    System.out.print(ANSI_CLS + ANSI_HOME);
    System.out.flush();     

Try this.

This works well for me. (Kubuntu)

ghchoi
  • 4,812
  • 4
  • 30
  • 53