Results 1 to 5 of 5
  1. #1
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default 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:
    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("");
        }
    
    
        }
    Thank you for any help.
    Last edited by erikjd21; 04-17-2011 at 07:59 AM. Reason: commented where errors were

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Have you tried declaring the variables outside the method?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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 
    }

  4. #4
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    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.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You're welcome. Glad to have helped. Please mark your thread solved with the thread tools at the top of the page.

Similar Threads

  1. Replies: 18
    Last Post: 04-18-2011, 05:39 AM
  2. Replies: 3
    Last Post: 02-11-2011, 02:40 AM
  3. Final variable-constructor method.
    By D4rkNrG in forum New To Java
    Replies: 9
    Last Post: 10-13-2010, 11:18 AM
  4. Not recognizing a variable in a method
    By Jamison5213 in forum New To Java
    Replies: 4
    Last Post: 01-03-2010, 08:04 PM
  5. Renaming a method/variable
    By gapper in forum Eclipse
    Replies: 0
    Last Post: 01-31-2008, 01:29 PM

Posting Permissions

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