1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Small Java Problem

Discussion in 'Mac Programming' started by finnschi, Nov 25, 2010.

  1. macrumors 6502

    #1
    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 ...

    Code:
    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
     
  2. macrumors 68040

    lee1210

    #2
    Code:
    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
     
  3. macrumors 6502

    #3
    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

    Code:
    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 );
    				
    			}		
    			
    		}
    }
    }
    
     
  4. macrumors 68040

    lee1210

    #4
    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
     
  5. macrumors 6502

    #5
    That didn't do it

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

    any other way to do it?
     
  6. macrumors 6502

    #6
    I decided it would be cooler to have it display it every second :D
     
  7. macrumors 68040

    lee1210

    #7
    Oops, didn't even look at the code. Declare currentmoney before the loop:
    double currentmoney = 0.;

    Then in the loop:
    currentmoney += mps;

    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
     
  8. macrumors 6502

    #8
    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
     
  9. macrumors 68040

    lee1210

    #9
    Substitute print with println.

    -Lee
     
  10. macrumors 6502

    #10
    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
     

Share This Page