Results 1 to 7 of 7
  1. #1
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default Variable not initialized after while loop

    I have some code that contains the variable hoursWorked. My code has the user input the hoursWorked and it functions. Now I want to put this line of code into a while loop to make loop until the entered value is >0. When I do this the variable hoursWorked which is used after this while statement now thinks it isn't initialized after the while statement. Why would adding the while loop make the program think the hoursWorked varialble isn't initialized later on in the code?

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    it would help if we saw your code... and if your variable is an int, i think it's automatically initialized to 0.

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  4. #4
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default Added section of code.

    double hoursWorked;
    double hourlyRate;
    double sum;
    String employeeName = "null";

    while(!employeeName.equals("stop")) {

    System.out.println("Enter Employee Name"); //prompt
    employeeName = input.nextLine(); //read first number from user

    if (employeeName.equals("stop"))
    break;
    int verify = -1;
    while(verify <0) {
    System.out.print( "Enter Hours Worked: " ); // prompt
    hoursWorked = input.nextDouble(); // read second number from user
    if (hoursWorked <0)
    System.out.println("Please enter a positive number");
    else
    verify = 1;
    }
    System.out.print( "Enter Hourly Rate: " ); // prompt
    hourlyRate = input.nextDouble(); // read second number from user

    sum = hourlyRate*hoursWorked;

  5. #5
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default

    No I declare it before the while loop.

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    it is possible that your code may not enter the while loop
    hoursWorked may not assign to any value,
    i suggest you to initialize it to 0

  7. #7
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default

    That was it since I hadn't assigned a value to hoursWorked it was causing the error when I tried to use it in a calculation later. THANKS A BUNCH.

Similar Threads

  1. [SOLVED] &quot;start: applet not initialized&quot;
    By DenniGa in forum Java Applets
    Replies: 3
    Last Post: 02-24-2009, 02:10 AM
  2. [SOLVED] Referencing Variable within Loop
    By Judoon_Platoon in forum Java Applets
    Replies: 2
    Last Post: 05-22-2008, 11:54 AM
  3. getting the value of variable
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 03-05-2008, 01:42 AM
  4. Getting variable value from a variable name
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:26 PM
  5. Not initialized variable
    By bbq in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:02 AM

Posting Permissions

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