Results 1 to 4 of 4
  1. #1
    BornToCode is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Cant figure out why my program wont calculate

    i m trying to make a work calculator, and use min wage,
    I m stuck on getting my program to calculate, it looks like it would work to me, but it terminates after i input everything.
    Any help would be appreciated

    Java Code:
    package WorkClock.Pratice;
    import java.util.Scanner; 
    public class WorkClock {
    	// call the main method to run the program
    	 public static void main(String args[]) 
    	 {  
    		WorkClock workObj = new WorkClock();
    		workObj.getfirstName();
    		workObj.getlastName(); 
    		workObj.gettimeWorked();
    		workObj.calaculate(); 
    	 }
    // define the data we need
    	private String firstName;
    	private String lastName;
    	private int timeWorked;
    	private int totalTime;
    	Scanner user_input = new Scanner(System.in);
    // create the constructor
    	 public WorkClock()
    	 {
    		 
    	 }
    	 
    // create the get and set fields 
    	 public String getfirstName()
    	 {
    		 System.out.println("Please Input Your First Name");
    		 firstName = user_input.next();
    		 return firstName;
    	 
    	 }
    	 public String getlastName() 
    	 {
    		 System.out.println("Please Input Your Last Name");
    		 lastName = user_input.next();	
    		 return lastName; 
    	 }
    	 public int gettimeWorked()
    	 {
    		 System.out.println("Please Input Your Time Worked");
    		 timeWorked = user_input.next();	
    		 return timeWorked;
    	 }
    	 public void setfirstName(String newfirstName)
    	 {
    		 firstName = newfirstName; 
    	 }
    	 public void setlastName(String newlastName)
    	 {
    		 lastName = newlastName; 
    	 }
    	 public void setTime(int newtimeWorked)
    	 {
    		 timeWorked = newtimeWorked;
    	 }
    	 public int calaculate()
    	 {
    		 totalTime = timeWorked * 10; 
    		 return totalTime; 
    	 }
    }
    Last edited by BornToCode; 07-11-2013 at 04:17 AM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Cant figure out why my program wont calculate

    There is a compile error in your code. timeWorked is an int variable, to get a user input use nextInt() method. And you should print out the calculated result.

  3. #3
    BornToCode is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Re: Cant figure out why my program wont calculate

    Hey thanks, fixed that, but it still keeps terminating after every run mpile
    Java Code:
    	 public int gettimeWorked()
    	 {
    		 System.out.println("Please Input Your Time Worked");
    		 timeWorked = user_input.nextInt();	
    		 return timeWorked;
    	 }
    Also changed this:
    Java Code:
    	 public int calaculate()
    	 {
    		 totalTime = timeWorked * 10; 
    		 System.out.printf("Hours: ", totalTime);
    		 return totalTime;
    	 }
    dunno why X.X?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Cant figure out why my program wont calculate

    First, your program will only run once because it enters the main entry point, executes the methods and then exits. After prompting and getting the input, you invoke the calculate method but you don't capture the return value.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 5
    Last Post: 02-19-2013, 02:16 AM
  2. Can't compile simple program to calculate primenumber
    By Daniel Silvester in forum New To Java
    Replies: 4
    Last Post: 04-06-2012, 04:07 AM
  3. Program in Java To calculate GCD of n numbers.?
    By ankitsinghal_89 in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 09:23 AM
  4. Program compiles but wont run to text file...
    By marylanddem in forum New To Java
    Replies: 2
    Last Post: 12-05-2010, 04:05 PM
  5. Why my program cannot calculate the decimal value?
    By pearllymary78 in forum New To Java
    Replies: 4
    Last Post: 06-23-2008, 12:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •