0

I have a college assignment where I have to get a first, middle, and last name from the user, and their age, and give them a bank ID using the initials and whatnot. But that's not what I'm here to ask.

I wanted to have a little bit of fun with it, and make the "Imaginary Bank" accidentally tell the user that it's a scam! Then an Error will pop up and delete that accidental line of text, replacing it with the normal "We look forward to helping you!" line. All I need to know how to do is delete that line of text that starts with "At Imaginary Bank, we" Thanks!

        System.out.println("Hello " + first_name + " " + last_name + ", greetings from the Imaginary Bank!");
        System.out.println("To access your account, please use the following ID: " + first_init + middle_init + last_init + age);

        System.out.println("At Imaginary Bank we look forward to scamming you and stealing your money!");

    try 
    {
        Thread.sleep(11000);
    } 
    catch (InterruptedException e) {
        e.printStackTrace();}
        System.out.println("ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR!");

    try
    {
        Thread.sleep(3000);
    }
    catch (InterruptedException e) {
        e.printStackTrace() ;}

        System.out.println("We look forward to aiding you with your financial needs! - The IB Team");
Yu Hao
  • 119,891
  • 44
  • 235
  • 294

3 Answers3

1

According to this answer, you can print a backspace character using \b in the console using System.out.print. Therefore, for however many characters you have previously printed, print that many backspace characters.

Additionally, this answer for the same question suggests using the cls command to clear the console output entirely, however this forever binds your application to only operating systems that use that command (In this case Windows / Dos). In linux, for example, the command is clear...I'm sure you see the potential problem.

Community
  • 1
  • 1
Paul Richter
  • 10,908
  • 10
  • 52
  • 85
  • Is there a way to do that AFTER the Error pops up? Like with the "We look forward to" line. Is there a way to reference that line LATER in code rather than right after (or rather, before any other system.out.prints)? – user2758205 Sep 08 '13 at 05:45
  • @user2758205 No unfortunately not. The console is nothing more than a dump for whatever was sent to the standard output device, in this case, either the eclipse console, or your operation system's terminal window. Anything you do with regards to either of those suggestions will essentially effect the last line/characters. I suppose you could keep a count of the number of characters written to the console (including the error message), and then use the backspace trick tp wipe out that number of characters, and then rewrite the error line AND anything else that followed which you wanted to keep. – Paul Richter Sep 08 '13 at 05:53
0

You can always print out backspace characters like this:

System.out.print("\b");

Just print out the same number of characters you would like to remove.

overloading
  • 1,210
  • 4
  • 25
  • 46
  • For whatever reason, the line of code is essentially being ignored. Nothing is being backspaced. (Of course, I'm probably doing something wrong) – user2758205 Sep 08 '13 at 05:59
  • @user2758205 How did you write it in your code? Note that you'd need to write X number of `\b` in the String, where X is the number of characters to delete. If you only wrote one, you might not see anything if the deleted character was a space. – Paul Richter Sep 08 '13 at 06:04
  • Haha yes, I put in plenty \b 's for something to show up. I put it immediately before the "System.out.println(ERROR!...." line, and tried putting it immediately after the line I wanted to delete. – user2758205 Sep 08 '13 at 06:07
  • @user2758205 Try using `print` instead of `println` for your error message. Can't backspace beyond a new line character, which is what `println` inserts at the end of the string. – Paul Richter Sep 08 '13 at 15:05
0

Check out How to delete stuff printed to console by System.out.println()? There is not a certain way to remove text from the output window but there is generally a way for each type of console window. Take your pick for what works best for your deployment.

Community
  • 1
  • 1
ug_
  • 11,267
  • 2
  • 35
  • 52