PDA

View Full Version : Small Java Problem




finnschi
Nov 25, 2010, 03:33 PM
Hey guys i am planning on Programming a little Java Programm, that shows the amount of money earned in a certain time but i don't know how to Programm something that updates itself every 30 seconds or so...


1. User input : "how much money do you earn per hour"

2. calculate money earned per second

3. Display Current Amout earned every 30 seconds


i would start like this ...


package countdownpack;
import java.util.Scanner;





public class countdown {

// TODO Auto-generated method stub


static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {


System.out.print("how much Money do you earn in 1 Hour?");
double money = sc.nextDouble();

double mps = (money/3600);
System.out.print("you earn"+ mps + " Money per second" );

System.out.print("Start Working now? yes/no ");
String s1 = sc.next();
char answer = s1.charAt(0);

if (answer == 'y'); {

????????????????????????????????????????



}else {
exit();
System.out.println("END");






}
}
}










Maybe you guys can help me out here :D



lee1210
Nov 25, 2010, 03:39 PM
while(true) {
//calculate and display
Thread.sleep(30000);
}

Put that in where your ????? is, and fill in the calculation and display.

You can wrap the sleep in a try-catch, but probably not needed at this stage.

-Lee

finnschi
Nov 25, 2010, 03:56 PM
Wow thank you for your quick reply, but it didn't quite work, I had to wrap it but thats okay! its almost perfect, but now I need to know how I can tell the Programm to re calculate (add 1 mps each time it displays the current amount earned, right now its just displaying the same value over and over again... :D


package countdownpack;
import java.util.Scanner;

public class countdown {

// TODO Auto-generated method stub


static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {


System.out.print("how much Money do you earn in 1 Hour?");
double money = sc.nextDouble();

double mps = (money/3600);
System.out.print("you earn"+ mps + " Money per second" );

System.out.print("Start Working now? yes/no ");
String s1 = sc.next();
char answer = s1.charAt(0);

if (answer == 'y'); {

while(true) {
//calculate and display
double currentmoney= (+mps);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


System.out.print("Your current money earned is "+ currentmoney );

}

}
}
}

lee1210
Nov 25, 2010, 04:00 PM
Change:
double currentmoney= (+mps);
to:
double currentmoney+= mps;

If you want to do what you said originally:
double currentmoney+= mps*30.;
and change the sleep to 30000.

-Lee

finnschi
Nov 25, 2010, 04:03 PM
That didn't do it

"Syntax error on token "+=", = expected "

any other way to do it?

finnschi
Nov 25, 2010, 04:04 PM
Change:


If you want to do what you said originally:
double currentmoney+= mps*30.;
and change the sleep to 30000.

-Lee

I decided it would be cooler to have it display it every second :D

lee1210
Nov 25, 2010, 04:06 PM
Oops, didn't even look at the code. Declare currentmoney before the loop:
double currentmoney = 0.;

Then in the loop:
currentmoney += mps;

I decided it would be cooler to have it display it every second :D

Nothing stresses a user out than a program updating too fast. You're just getting started, and 1 second isn't too fast, but at a call center job I had the call tracking software could update the call length every millisecond. It was devastating to your psyche if you hit that accidentally.

-Lee

finnschi
Nov 25, 2010, 04:14 PM
Oops, didn't even look at the code. Declare currentmoney before the loop:
double currentmoney = 0.;

Then in the loop:
currentmoney += mps;

-Lee

OMG that totally worked :D

now just 1 more things

can I somehow easily display currentmoney in the console using a new line for each new amount, right now its showing like this:

Your current money earned is 0.002777777777777778Your current money 333336Your current money earned is 0.13611111111111113

everything in 1 line I need to somehow add a <br> after each output..., i am coming from HTML as you can see... :rolleyes:


You are awesome btw

lee1210
Nov 25, 2010, 04:22 PM
Substitute print with println.

-Lee

finnschi
Nov 25, 2010, 04:26 PM
Substitute print with println.

-Lee

ha wow thank you I wanted to accomplish doing this for month.. but I finally got around doing it it feels great... first java Program Wohooo :D

Thank you so much for helping me !!!


if you know a good source for learning how i can now turn this into a little Java Applet , it would be GREAT !! I want to display it on a website so I can turn it On whenever i start working and have it running in the Background and take a peek every couple of Minutes to check how much I have earned :D