Clear The Console Programmatically In Java - java

I created a sample Java application. I want to clear the window options, i.e.:
Register
Login
Clear
If the user presses 3 I need to programmatically clear all options. Something like Console.clear?
Is there any way that I can do this with Java?

You will need to output a bunch of blank lines. Even in Windows/*nix, clear/cls doesn't truly clear the screen, it just prints enough blank lines that you cannot see the previous text.

You can try System.out.print("CLS");
Or use loops to clear the screen like
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
and then call this method clearScreen(); if you want to clear.
Sorry my english is bad. :) I just want to help you.

You mean you created a console application and want to clear the console (not necessarily the Eclipse console)?
If so then I guess you're looking for:
Runtime.getRuntime().exec("cls");
But be aware this will be system dependent.

If you are working with the console, then these might prove useful.
Using "backspace" character.
Using process builder.

Related

How to compare two identical methods in eclipse?

I'm doing android app from a book, for some reason the exact method source code I wrote myself does not work as expected and I am trying to debug it.
I have two exact chunks of code, my method and the sample method.
How to compare them in eclipse?
Select both files by clicking the first, then while holding CTRL click on the second.
Now both of them got selected.
Now click one of them (doesn't matter which one) with the right mouse button.
From the appearing context menu choose:
Compare
Each other
Now you can do a text compare.
Did I get right that neither of the sample method nor your method do what they should?
Then there are two possibilities why the code won't do what it should:
The book is obsolet
You made something wrong
either way, google for your specific problem, maybe someone else has encountered it as well and already solved it.
for your Question: already answered in another comment
use beyond compare, it's a great tool for comparing classes, and methods! download beyond compare

Intellij IDEA go to previous method shortcut

Do you know if IDEA has a shortcut which will bring me to the last method I was looking into, not necessary changing it, just looking into. I know that there is Ctrl+Alt+Left/Right but it is slightly different.
If you are on a Mac, press Command+Shift+A.
If you are on a Windows, press Ctrl+Shift+A.
This will bring up a searchable list of ALL of the possible shortcuts. Just type what you want to do and it'll come up there. Very handy. I use it all the time.
Hope that helps. Good luck :)
EDIT:
Just took a look myself. Doesn't look like there is anything like what you wanted. The closest thing is moving to the the next method or the previous method, but this is just in order of how you have them written in the code, not in the order that you look at them. Sorry!

Java check if user is typing in the console

Is there a way to check if the user is typing in the console window in Java? My program prints information it receives about client connections using System.out.print() and I want it to stop printing information temporarily while the user is typing. User input is read on a separate thread using the Scanner class. I need to be able to see if the user has typed anything and still have whatever the user has typed (if anything) available to the scanner. If it's possible I would like to avoid using external libraries and just stick with the java libraries.
No, there are no ways to do that directly using the console (that I'm aware of) - the content of the console will only get sent to the application once enter is pressed and will then be available to the Scanner.
One way to solve it is to make your own console, that you can read and write from. Then you'll be able to do anything you want really (including check if anything is highlighted and so on). If you don't know how to code a GUI, you should look into that. Oracle has a tutorial on GUI with Swing.
The thread that listens to the user console , Generates an interrupt when the user starts typing, while the other thread that prints the information need to reset the interrupt, all you have to do is implementation of the scenario.
I think you're looking for Thread.sleep(milliseconds);. Using this until the user is done, you should get what you want.
Next time, please elaborate on your questions.

Java command line exit command?

I'm making a server, and it is on a Text Based Raspberry Pi. basically, everything is running from the command line, so when the server runs, there is no graphics, and it prints everything out using System.out.println();. so my question is, instead of having a button that runs a shutdown() method, how can i make it so at any point in time, i am able to push, say, 'e', and the program will run the shutdown() method? i've done some searching, and am not sure quite how to phrase the question. i was thinking adding a keylistener, but im not sure if that can be added to nothing graphic? anyway, any help would be appreciated!!! thanks in advance
To register keyboard events you first need to have the focus on your program, and for that you need a gui. I suggest:
1.- Create a JLabel(and a scrollbar).
2.- Instead of using System.out.print("text");, use myJLabel.append("text" + "/n");.
3.- Add a keyboard listener. Register key events so that the x key closes your server, the s key stops it, ...
As you have described your app, you can't write input to the server, you can only read output from it. I recomend step 4.
4.-Add a JTextFiel to send input to the server.
5.-To make it user-friendly , you could use a JEditorPane instead of a JLabel, and add HTML to your output.
Alright, so based on the comments, (which i up voted btw), i made a thread that constantly used scanner to see if i typed "exit". Thanks for all the help!
If you don't want to have a visible gui, you could create an "always-focused invisible grafical interface" with the listener.

Simple Java alert for debugging purposes

I'm trying to debug/trace how some Java code is executed (or if it even is). I would like a simple way to do an alert just so I can tell how the code is being executed. Something like:
alert ("do we get here");
in javascript, or:
echo ("do we get here");
in php.
I have googled it but alot of the methods seem fairly complicated and an excessive amount of code. I am new to java, is there any way to achieve this?
JOptionPane.showMessageDialog(null, "Your Message");
The null part can actually be switched by a proper parent component, if there is one.
You can use
System.out.println("Here we are");
but if you want to know how we got there you can use
new Throwable("Here we are").printStackTrace(System.out);
and it will print the call stacka s well.
Sytem.out.println("do we get here");
If you just want to do text output to the screen, you could just use System.out.println("Point 1");. If you are after a more visual thing (which I am assuming you are), you could take a look at the JOptionPane class, with more information available here.
A more traditional (an most likely preferred) way of doing something like what you are after would be use Loggin. SLF4J is a relatively popular way of doing logging in a Java application.

Categories