Java: How to clear command line? Quick help plz...

Sharky Forums


Results 1 to 11 of 11

Thread: Java: How to clear command line? Quick help plz...

  1. #1
    Goldfish
    Join Date
    Jul 2001
    Location
    Norrköping, Sweden
    Posts
    68

    Unhappy Java: How to clear command line? Quick help plz...

    I'm making a program for one of my cs classes, that won't use the graphic interface. Instead it will "system.out.println()" a lot of text.

    My question is: How do you clear the command line, like 'cls', in Java?

    Thanks in advance!

    Edit: That would be 'console' instead of 'command line' I suppose.

    [This message has been edited by adamxyz (edited October 02, 2001).]

  2. #2
    Goldfish
    Join Date
    Jul 2001
    Location
    Norrköping, Sweden
    Posts
    68

    Post

    Found this one: http://www.rgagnon.com/javadetails/java-0047.html

    but that won't do, since I'm on both PC and Sun. Need something cross-platform...

  3. #3
    Catfish
    Join Date
    Jun 2001
    Posts
    130

    Post

    If your printing a lot of text to the console, you may want to consider just putting it into a file. Writing to a file in Java is more involved than in C++, but still pretty easy.

    ie
    PrintWriter fOut;
    fOut=new PrintWriter(new BufferedWriter(new FileWriter("fileName")));

    fOut.println("what you want to print");
    fOut.close();
    AMD 1.2GHz Athlon 266FSB w/GlobalWin CAK38
    Asus A7M266
    Kingston PC2100 2x 256MB
    IBM 75GXP ATA100 7200RPM 45GB
    Creative 12x DVD
    Yamaha 4x4x16x CD-RW
    Matrox G400 Millenium
    Matrox Rainbow Runner TV-Tuner/Capture Card
    SB Live! 5.1 Platinum
    Creative Dxr3 Decoder Card
    3Com 3C905B 10/100 NIC
    Mitsubishi 900u
    Antec PP403X 400W PSU
    In-Win Q500

  4. #4
    Tiger Shark
    Join Date
    Feb 2001
    Location
    Satan Country
    Posts
    564

    Post

    I'm fairly sure that to do a real cls would be a system call, and so would not be cross-platform compatible. I didn't check the link but I am guessing they called exec() at some point.

    You could always print a bunch of newlines, I guess, bt maybe a file would be better.

    ------------------
    system specs:
    Voodoo 5 5500 agp
    tyan 1834d tiger 133 dual 800eb 133mhz FSB
    via chipset 133 via apollo pro (don't make this mistake)
    256 MB RAM
    2 maxtor 60gig ata100 drives
    promise ata100 controller
    liteon 52 truex cdrom
    Linksys ethernet 10/100
    Soundblaster Live! (what's so exciting about it??) value edition
    300watt power supply (inwin)
    about 7 pounds of fans (I'm not kidding)
    Suse 7.1(god gnome is crappy compared to CDE)/win2000 based system
    I'm half Scottish and half French.

    I surrender to alcohol.

  5. #5
    Goldfish
    Join Date
    Jul 2001
    Location
    Norrköping, Sweden
    Posts
    68

    Post

    Originally posted by driver:
    If your printing a lot of text to the console, you may want to consider just putting it into a file. Writing to a file in Java is more involved than in C++, but still pretty easy.

    ie
    PrintWriter fOut;
    fOut=new PrintWriter(new BufferedWriter(new FileWriter("fileName")));

    fOut.println("what you want to print");
    fOut.close();
    How would printing the text to a file help me? I don't get it...

    The thing is, I want a console-based menu. After stepping through the menus for a while, the console will get "long", that's what I want to avoid.

    By printing the menu to a file first...what would I do with that? Even if I then output the file contents to the console, and clear the file after each output, the console will get cluttered.

    Or is it just me...

  6. #6
    Catfish
    Join Date
    Jun 2001
    Posts
    130

    Post

    I didn't realize you were creating a meu system, I was under the impression that you were just outputting a lot of information for the user to read, not neccessarily to interact with. My bad. Well, you may want to try and write a GUI interface for your program. It's pretty easy in Java and I believe it allows you to clear the display.
    AMD 1.2GHz Athlon 266FSB w/GlobalWin CAK38
    Asus A7M266
    Kingston PC2100 2x 256MB
    IBM 75GXP ATA100 7200RPM 45GB
    Creative 12x DVD
    Yamaha 4x4x16x CD-RW
    Matrox G400 Millenium
    Matrox Rainbow Runner TV-Tuner/Capture Card
    SB Live! 5.1 Platinum
    Creative Dxr3 Decoder Card
    3Com 3C905B 10/100 NIC
    Mitsubishi 900u
    Antec PP403X 400W PSU
    In-Win Q500

  7. #7
    Goldfish
    Join Date
    Jul 2001
    Location
    Norrköping, Sweden
    Posts
    68

    Post

    Yeah, I think I will have to make a GUI, not that it's harder. It's just that my teacher somehow seems to like console output. I'll just make a console-like GUI then.

    Thanks anyway!

  8. #8
    Tiger Shark
    Join Date
    Oct 2000
    Posts
    631

    Post

    You could query the OS and depending on the result execute the appropriate 'clear' command.

    To make it completely portable, you could create a file that has an OS token and the command it should execute.

    So, the file would look something like:

    Windows NT 4.0=cls
    Solaris=clear
    SomeOtherOs=blahblah

    Read the file in, parse it, and store it in a hashtable. Use System.getProperty("os.name") to get the name. Use this name to look up the command that you stored in the hashtable.

    To execute the string command you need to use the Runnable and Process classes.

    Good luck



    ------------------
    Blueskies Blackdeath
    Blueskies Blackdeath

  9. #9
    Goldfish
    Join Date
    Jul 2001
    Location
    Norrköping, Sweden
    Posts
    68

    Post

    Originally posted by alienrash:
    To execute the string command you need to use the Runnable and Process classes.
    Hmm, how would I go about doing that?
    I tried:

    Runtime.getRuntime().exec("cmd /c cls");

    but it didn't work. Does it perhaps run in a separate thread, which I don't know anything about, and therefore doesn't affect the current console that I'm outputting to. Starting a different program, like ...exec("notepad... works just fine, so there's obviously nothing wrong with the methods.

    Another thing, what's the command.com or cmd equivalent for Solaris?

  10. #10
    Tiger Shark
    Join Date
    Oct 2000
    Posts
    631

    Post

    DOH! You are right. It is spawning another thread in which case your app will not be affected.

    Originally posted by adamxyz:
    Hmm, how would I go about doing that?
    I tried:

    Runtime.getRuntime().exec("cmd /c cls");

    but it didn't work. Does it perhaps run in a separate thread, which I don't know anything about, and therefore doesn't affect the current console that I'm outputting to. Starting a different program, like ...exec("notepad... works just fine, so there's obviously nothing wrong with the methods.

    Another thing, what's the command.com or cmd equivalent for Solaris?


    ------------------
    Blueskies Blackdeath
    Blueskies Blackdeath

  11. #11
    Goldfish
    Join Date
    Jul 2001
    Location
    Norrköping, Sweden
    Posts
    68

    Post

    Originally posted by alienrash:
    DOH!
    Hehee. That's what I've been telling myself too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •