JAVA Problem

This is the place to talk about anything not related to Transport Tycoon itself.

Moderator: General Forums Moderators

Post Reply
User avatar
denny577
Engineer
Engineer
Posts: 111
Joined: 20 Nov 2005 16:10
Location: Netherlands

JAVA Problem

Post by denny577 »

How can I clear the output screen in JAVA? I made a clock but t just prints the time over and over again :P
Attachments
ClockTalk.txt
This is the code
(1.84 KiB) Downloaded 154 times
User avatar
Dropzone
Traffic Manager
Traffic Manager
Posts: 178
Joined: 04 Apr 2005 21:38
Location: North Lincs., UK

Re: JAVA Problem

Post by Dropzone »

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.
User avatar
denny577
Engineer
Engineer
Posts: 111
Joined: 20 Nov 2005 16:10
Location: Netherlands

Re: JAVA Problem

Post by denny577 »

DropZone 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.
I did that, but then it prints the time over and over without an empty line in between :P
Attachments
example.JPG
Screen of the problem
(131.25 KiB) Downloaded 34 times
Sacro
Tycoon
Tycoon
Posts: 1145
Joined: 18 Jun 2005 21:08
Location: Here
Contact:

Re: JAVA Problem

Post by Sacro »

Code: Select all

for (int time = 0; time < 2; time++) {
    		time--;
:lol:

woud

Code: Select all

for(;;)
not be better?
We Am De Best

Host of ThroughTheTube site
User avatar
denny577
Engineer
Engineer
Posts: 111
Joined: 20 Nov 2005 16:10
Location: Netherlands

Re: JAVA Problem

Post by denny577 »

Sacro wrote:

Code: Select all

for (int time = 0; time < 2; time++) {
    		time--;
:lol:

woud

Code: Select all

for(;;)
not be better?
When I try that it says that in

Code: Select all

Calendar now = Calendar.getInstance();
now is unreachable code

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;
(Only I don't know what to say for backspace :( )
then before the clock

Code: Select all

        System.out.print( bspace );
Attachments
ClockTalk.txt
Again
(1.84 KiB) Downloaded 146 times
User avatar
Dropzone
Traffic Manager
Traffic Manager
Posts: 178
Joined: 04 Apr 2005 21:38
Location: North Lincs., UK

Re: JAVA Problem

Post by Dropzone »

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).
User avatar
Lakie
TTDPatch Developer
TTDPatch Developer
Posts: 1799
Joined: 26 May 2004 16:37
Location: Britain
Contact:

Re: JAVA Problem

Post by Lakie »

Sacro wrote:

Code: Select all

for(;;)
not be better?
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...
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."
User avatar
denny577
Engineer
Engineer
Posts: 111
Joined: 20 Nov 2005 16:10
Location: Netherlands

Re: JAVA Problem

Post by denny577 »

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).
Didn't think of that, it does work indeed.
Lakie wrote:
Sacro wrote:

Code: Select all

for(;;)
not be better?
In C++ at least, "while (1)" would be much better
When I try that it says "cannot convert from int to boolean", but my version did work fine anyway :lol:

I attached the working version :P
Attachments
ClockTalk.txt
The current working version
(1.92 KiB) Downloaded 143 times
User avatar
Dropzone
Traffic Manager
Traffic Manager
Posts: 178
Joined: 04 Apr 2005 21:38
Location: North Lincs., UK

Re: JAVA Problem

Post by Dropzone »

denny577 wrote:
Lakie wrote: In C++ at least, "while (1)" would be much better
When I try that it says "cannot convert from int to boolean", but my version did work fine anyway :lol:
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.
User avatar
Steve
Tycoon
Tycoon
Posts: 2085
Joined: 10 Jan 2004 20:19
Location: London
Contact:

Re: JAVA Problem

Post by Steve »

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.
User avatar
denny577
Engineer
Engineer
Posts: 111
Joined: 20 Nov 2005 16:10
Location: Netherlands

Re: JAVA Problem

Post by denny577 »

DropZone wrote:
denny577 wrote:
Lakie wrote: In C++ at least, "while (1)" would be much better
When I try that it says "cannot convert from int to boolean", but my version did work fine anyway :lol:
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.
It works thank you :P
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 :P
User avatar
denny577
Engineer
Engineer
Posts: 111
Joined: 20 Nov 2005 16:10
Location: Netherlands

Re: JAVA Problem

Post by denny577 »

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.
User avatar
Daan Timmer
Tycoon
Tycoon
Posts: 2406
Joined: 12 Apr 2004 11:21
Location: +31299......

Re: JAVA Problem

Post by Daan Timmer »

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.
Do you want to read it from the console? or do you want to read it from a KeyListener?
(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){}
});
}
(console)

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;
}
}
sorry for no indention, typed it just in here :P
Image

"Your arguments are totally nothing.. They have so little meaning that its kind of funny you took the time to write them"
"your pixels aren't good enough to talk to me.. sorry"
"Random does not mean fair, and past results do not influence future occurrences." - Corbenn
Post Reply

Return to “Off-Topic”

Who is online

Users browsing this forum: No registered users and 20 guests