• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

Clearing the console

Hi, Im mofiying a friends program that finds its way through a maze, and would like to know how to clear the console in java so when I reprint the maze for every step the old step doesnt appear right above it, thanks!
0
suns0ft
Asked:
suns0ft
  • 7
  • 5
  • 2
1 Solution
 
CEHJCommented:
Depends on how you're doing this maze, but this might give you a clue as to a possible approach:

public class TestPercent {

      public static void main(String[] args) throws Exception {
            PercentageDisplayer pd = new PercentageDisplayer(500);
            for(int i = 1;i <= 5;i++) {
                  pd.update(100);
                  Thread.sleep(500);
            }
      }

      static class PercentageDisplayer {
            long extent;
            long received;

            public PercentageDisplayer(long extent) {
                  this.extent = extent;
            }

            public void update(long increment) {
                  for(int i = 0;i < 12;i++) {
                        System.out.print('\010');
                  }
                  received += increment;
                  long percentage = 100 * received / extent;
                  System.out.print("" + percentage + "% received");
            }
      }
}
0
 
suns0ftAuthor Commented:
ummm Im not a brilliant programmer, but does that at any point clear the console?
0
 
lwinkenbCommented:
for(int i = 0;i < 12;i++) {
                    System.out.print('\010');
}

That is the part of the code CEHJ posted that clears the console.  There is no way to clear the console in java like there is in C or C++.  Instead what programmers usually do is put a System.out.println(); in a for loop to push all the text off the page.  This give the appearance that the console is actually being cleared.  
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
Otherwise, you'd have to get hold of a JNI-based API to control the console. I only know of one for Unix
0
 
lwinkenbCommented:
There is no API function to clear the console in windows, but you can use
system("cls");
to clear the windows console.
0
 
suns0ftAuthor Commented:
thanks, the program is at uni, so I'll try it out tomorrow morning and hand out the points :)
0
 
suns0ftAuthor Commented:
Ok I tried both methods, and while the one CEHJ suggested works, it doesnt look very good on the screen. I tried to use system("cls"); and I got a compiler error, is there anything else I need to do to get the one working?
0
 
lwinkenbCommented:
system("cls") cant be run from java.  You have to write it in a small c++ method that looks like:

#include <jni.h>
#include "MyClass.h"
#include <stdio.h>

JNIEXPORT void JNICALL
Java_MyClass_clearScreen(JNIEnv *env, jobject obj)
{
    system("cls");
    return;
}

build that code into a dll file, then call the dll from your java program.  If you want help doing that, I can help you do it.  It's a little bit more work than CEHJ's method though.



0
 
suns0ftAuthor Commented:
cool, Im not sure how to do that though, if you want to help me i'll increase the point count as well :D
0
 
lwinkenbCommented:
Ok, I will show you the steps to do this.

First let's create a Java test class:

class ConsoleProg {
      public native void cls();

    static {
        System.loadLibrary("clearconsole");
    }
   
    public static void main(String[] args) throws Exception{
        System.out.println("Some text here!");
        System.out.println("Clearing the console in 5 seconds...");
        Thread.sleep(5000);
        new ConsoleProg().cls();
        System.out.println("Console is cleared");
    }
}

There are two things to note in this class.  First is the command System.loadLibrary().  This basically loads a dll file into memory (in this case clearconsole.dll).  The second thing is the declaration public native void cls().  This is a function inside the clearconsole.dll file we are going to call.

Now compile that class.

Now we need to make a header for the java class.  To do this, just run javah.  For the above class we run:
javah ConsoleProg

Now you should see a file called ConsoleProg.h

Now we need to create the clearconsole.dll file.  To make a dll file that a java program can call, the function declaration must match what is inside the ConsoleProg.h file.  In our case the function declaration looks like:
JNIEXPORT void JNICALL Java_ConsoleProg_cls(JNIEnv *env, jobject obj)
Notice how the function declaration matches our java class.  It is in the form of:
JNIEXPORT <return cls.  void in our case> JNICALL Java_<Java class name>_<function name>(...)

Ok, now we write the code.

#include <jni.h>
#include "ConsoleProg.h"
#include <stdio.h>

JNIEXPORT void JNICALL
Java_ConsoleProg_cls(JNIEnv *env, jobject obj)
{
    system("cls");
    return;
}

Now we need to compile this into a dll file.  To do that, you need a c/c++ compiler.  You can get a free one from Borland, or the GNU compiler is free as well.  Make sure you compile it as a shared library file (.dll, .so, .o).  

The command to do this using Visual C++ compiler is:
cl -Ic:\java\include -Ic:\java\include\win32 -LD clearconsole.c -Feclearconsole.dll
for the GNU compiler it would look something like:
cc -G -Ic:\java\include -Ic:\java\include\win32  clearconsole.c -o clearconsole.dll

Now just run the java file like normal, and everytime you call cls(), the console will be cleared!
0
 
suns0ftAuthor Commented:
That looks very complete! I'll try it tomorrow (before my exam lol). And a complier isnt a problem,  the uni runs codewarrior, has java, c, c++ compilers etc. Also has visual C++ so if I cant work the codewarrior one I can follow your instructions.

Anyone have advice on how many points I should give? As it seems lwinkenb has gone to considerable effort to help me here :) Current points at 50, I was thinking of raising it to 100-150 if it works, seem fair?
0
 
suns0ftAuthor Commented:
Thanks admin, I'd forgotten about this question, I WAS waiting for someone to give some sort of recommendation as to how many points I should give out (I thought 50 was a little low, wanted some feedback before accepting, as I said in last post), but noone replied , and for some reason I never got a email when Venabili posted his cleanup.. :(

Reading through the answers I see that 50 points is a little low, are you able to add a few more to it after accepting an answer?
0
 
suns0ftAuthor Commented:
Cool, well lwinkenb went to a lot of effort helping me, so I think raising the points to 100 seems like a fair bonus, thanks :)
0
 
lwinkenbCommented:
=)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now