Results 1 to 4 of 4
- 07-11-2013, 04:02 AM #1
Member
- Join Date
- Jul 2013
- Posts
- 2
- Rep Power
- 0
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.
- 07-11-2013, 04:38 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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.
Website: Learn Java by Examples
- 07-11-2013, 04:46 AM #3
Member
- Join Date
- Jul 2013
- Posts
- 2
- Rep Power
- 0
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; }
Java Code:public int calaculate() { totalTime = timeWorked * 10; System.out.printf("Hours: ", totalTime); return totalTime; }
- 07-11-2013, 05:25 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Program to calculate sum of n integers need help
By abi in forum New To JavaReplies: 5Last Post: 02-19-2013, 02:16 AM -
Can't compile simple program to calculate primenumber
By Daniel Silvester in forum New To JavaReplies: 4Last Post: 04-06-2012, 04:07 AM -
Program in Java To calculate GCD of n numbers.?
By ankitsinghal_89 in forum New To JavaReplies: 4Last Post: 02-15-2011, 09:23 AM -
Program compiles but wont run to text file...
By marylanddem in forum New To JavaReplies: 2Last Post: 12-05-2010, 04:05 PM -
Why my program cannot calculate the decimal value?
By pearllymary78 in forum New To JavaReplies: 4Last Post: 06-23-2008, 12:52 AM
Bookmarks