Results 1 to 5 of 5
- 04-17-2011, 07:58 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 27
- Rep Power
- 0
Initializing a variable from another method
All other aspects of this assignment work perfectly, the final part is to have the weekly wage figured in a separate method. I have created the method, but I get the error that the variables are not initialized. When I attempt to initialize the variables by giving them values, they do not pull the values from the prior method, so the final output is wrong. You guys have helped me a ton so far, and Im sure this is something very simple I am overlooking. But I have been googling for hours now.
Here is the code:Thank you for any help.Java Code:package payrollwk4;//This is the project for the payroll checkpoint. import java.util.Scanner;//This imports the utility "Scanner" for use by the program. public class Employee { public void getInfo() { Scanner input = new Scanner(System.in); String name; double hours; double wages; boolean stop = false; while (!stop) { System.out.println("Welcome to Payroll manager!"); System.out.println("Please enter stop when you are finished"); System.out.println(""); System.out.print("Please enter the Employee's Name:"); name = input.next(); System.out.print(""); if (name.equals("stop")) { System.out.println("Thank you for using payroll manager!"); stop = true; input.close(); break; } else { System.out.print( "Now enter the number of hours " + name + " has worked:"); boolean negHours; do { hours = input.nextDouble(); negHours = hours < 0; if (negHours) { System.out.print("Please enter a positive number:"); } }while (negHours); System.out.print("Now enter " + name + "'s hourly wage:$"); boolean negWages; do { wages = input.nextDouble(); negWages = wages < 0; if (negWages) { System.out.print("Please enter a positive number:"); } }while (negWages); } calcPay(); } } public void calcPay() { double weeklypay; String name; double hours; double wages; weeklypay = (hours * wages);// error here System.out.println(name + " will get paid " + weeklypay + " this week."); // and here System.out.println(""); } }Last edited by erikjd21; 04-17-2011 at 07:59 AM. Reason: commented where errors were
- 04-17-2011, 08:13 AM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Have you tried declaring the variables outside the method?
- 04-17-2011, 08:14 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The employee class should have instance variables. Don't declare them in methods. They won't exist outside of the method. Calculate pay throws an error because you are declaring new local variables for the employees class info and not initializing them.
Java Code:class Employee{ instance variables constructor to instantiate instance variables calculate wage method uses the instance variables }
- 04-17-2011, 08:40 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 27
- Rep Power
- 0
Thank you Sunde887, I knew it was something stupid simple. I apologize for the stupid questions, this is all very new to me.
Iron, thank you for the reply.
I needed to declare the variables outside of the methods, rather than inside of both of them.
Works perfect now.
Thanks again guys.
- 04-17-2011, 08:55 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Can i Pass a variable as a parameter from one method to other method
By Anagha in forum New To JavaReplies: 18Last Post: 04-18-2011, 05:39 AM -
How can you use a variable in a method that is declared in main?
By drhinri in forum New To JavaReplies: 3Last Post: 02-11-2011, 02:40 AM -
Final variable-constructor method.
By D4rkNrG in forum New To JavaReplies: 9Last Post: 10-13-2010, 11:18 AM -
Not recognizing a variable in a method
By Jamison5213 in forum New To JavaReplies: 4Last Post: 01-03-2010, 08:04 PM -
Renaming a method/variable
By gapper in forum EclipseReplies: 0Last Post: 01-31-2008, 01:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks