JAVA Problem
Moderator: General Forums Moderators
JAVA Problem
How can I clear the output screen in JAVA? I made a clock but t just prints the time over and over again
- Attachments
-
- ClockTalk.txt
- This is the code
- (1.84 KiB) Downloaded 154 times
Re: JAVA Problem
It's the \n in the last print call that's preventing it from working properly. There are two ways you could fix it. The first way would be to remove the \n from that last print call, so it just looks like System.out.print(".");. The second way would be to instead change that \n to a \r, at which point you'd no longer need the System.out.print("\r"); call near the start of the program.
Re: JAVA Problem
I did that, but then it prints the time over and over without an empty line in betweenDropZone wrote:The second way would be to instead change that \n to a \r, at which point you'd no longer need the System.out.print("\r"); call near the start of the program.
- Attachments
-
- example.JPG
- Screen of the problem
- (131.25 KiB) Downloaded 34 times
Re: JAVA Problem
Code: Select all
for (int time = 0; time < 2; time++) {
time--;
woud
Code: Select all
for(;;)
We Am De Best
Host of ThroughTheTube site
Host of ThroughTheTube site
Re: JAVA Problem
When I try that it says that inSacro wrote:Code: Select all
for (int time = 0; time < 2; time++) { time--;
woudnot be better?Code: Select all
for(;;)
Code: Select all
Calendar now = Calendar.getInstance();
Otherwise it might help if someone could tell me how to get the backspace command into a variable, then I can let it print that before it prints the again
Something like this:
Code: Select all
char bspace = (char) 8;
then before the clock
Code: Select all
System.out.print( bspace );
- Attachments
-
- ClockTalk.txt
- Again
- (1.84 KiB) Downloaded 146 times
Re: JAVA Problem
Ah, the problem here is Eclipse. It seems that its console view doesn't respond correctly to \r (or \b, which is how you put a backspace in a string). There's probably no way to get this to work in Eclipse's console, although it does work fine if you run the program in a Windows console (cmd.exe).
Re: JAVA Problem
In C++ at least, "while (1)" would be much better, the syntax across the languages (C+ and Java) should be pretty much the same too...Sacro wrote:not be better?Code: Select all
for(;;)
Also note that is not "<iteration opener>;" - ( " for(;;); " ), it should surely be " for(;;) { "!
Note that I only know basic C++ so it may or may not work correctly in Java, but most of the examples our teacher gave us for C++ loops were written in Java, oddly enough, apparently because the syntax is the same or extremely close...
~ Lakie
TTDpatch Developer 2005 - 2010 ~ It all started because of shortened vehicle not loading correctly, now look where I've gone with it!
Grfs coded ~ Finnish Train Set (Teaser) | Bm73 (Release 3) | Emu 680 (Release 3)| Glass Station (Release 1) | UK Roadset (Version 1.1a) | New Water Coasts (Version 7)
Pikka: "Lakie's a good coder, but before he'll add any feature to TTDP you have to convince him that you're not going to use it to destroy the world as we know it."
Grfs coded ~ Finnish Train Set (Teaser) | Bm73 (Release 3) | Emu 680 (Release 3)| Glass Station (Release 1) | UK Roadset (Version 1.1a) | New Water Coasts (Version 7)
Pikka: "Lakie's a good coder, but before he'll add any feature to TTDP you have to convince him that you're not going to use it to destroy the world as we know it."
Re: JAVA Problem
Didn't think of that, it does work indeed.DropZone wrote:Ah, the problem here is Eclipse. It seems that its console view doesn't respond correctly to \r (or \b, which is how you put a backspace in a string). There's probably no way to get this to work in Eclipse's console, although it does work fine if you run the program in a Windows console (cmd.exe).
When I try that it says "cannot convert from int to boolean", but my version did work fine anywayLakie wrote:In C++ at least, "while (1)" would be much betterSacro wrote:not be better?Code: Select all
for(;;)
I attached the working version
- Attachments
-
- ClockTalk.txt
- The current working version
- (1.92 KiB) Downloaded 143 times
Re: JAVA Problem
If you wanted to do it that way in Java, you'd have to use while (true) { instead of while (1) { , because Java is more strict about type checking than C++ is. C++ lets you use integers and booleans interchangeably, because they're really the same type. In Java, they're two completely separate types, and you can't use one where it expects you to use the other. Doing it that way won't really make any difference to how the program runs, but Lakie's right that it's usually considered the neatest way of writing it.denny577 wrote:When I try that it says "cannot convert from int to boolean", but my version did work fine anywayLakie wrote: In C++ at least, "while (1)" would be much better
Re: JAVA Problem
Java is stongly typed, so 1 is not equivalent to true, as it would be in many scripting languages like Perl or Python.
while (true) { } would work just dandily, but I don't see any problem with for (;;) { }.
Backspace characters only work in some console environments those which are tty-like (teletype). This includes the usual console, but not a file. And apparently not eclipse.. System.out should have a function to check this, so you can alter your output accordingly.
while (true) { } would work just dandily, but I don't see any problem with for (;;) { }.
Backspace characters only work in some console environments those which are tty-like (teletype). This includes the usual console, but not a file. And apparently not eclipse.. System.out should have a function to check this, so you can alter your output accordingly.
Re: JAVA Problem
It works thank youDropZone wrote:If you wanted to do it that way in Java, you'd have to use while (true) { instead of while (1) { , because Java is more strict about type checking than C++ is.denny577 wrote:When I try that it says "cannot convert from int to boolean", but my version did work fine anywayLakie wrote: In C++ at least, "while (1)" would be much better
and as for the backspace, it works on the command line, only if I take out the \n it becomes very unstructured in Eclipse when I run it, it prints everything after each other, making it very hard to terminate
Re: JAVA Problem
Now I'm trying to make a program which saves the key you press into a variable, but the only way I know is getKey() but it says it cannot resolve and I don't know if I should use any imports or which, atm I have java.lang.* and java.util.*. I don't have a clue anymore so if anyone could help me out, I'd be grateful.
- Daan Timmer
- Tycoon
- Posts: 2406
- Joined: 12 Apr 2004 11:21
- Location: +31299......
Re: JAVA Problem
Do you want to read it from the console? or do you want to read it from a KeyListener?denny577 wrote:Now I'm trying to make a program which saves the key you press into a variable, but the only way I know is getKey() but it says it cannot resolve and I don't know if I should use any imports or which, atm I have java.lang.* and java.util.*. I don't have a clue anymore so if anyone could help me out, I'd be grateful.
(KeyListener)
Code: Select all
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
class AFrame extends JFrame{
public AFrame(){
addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent ke){}
public void keyReleased(KeyEvent ke){}
public void keyTyped(KeyEvent ke){}
});
}
Code: Select all
import java.util.Scanner;
class AClass{
public AClass(){
Scanner in = new Scanner(System.in);
String tmpStr = in.nextLine();
in.close();
//do something with tmpStr;
}
}
Who is online
Users browsing this forum: No registered users and 20 guests