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

    Default Accessing a variable from a loop

    Hello,
    I have a programming assignment ive spent the better part of today working and I am having some trouble. I have to make a program that reads data from a text document containing employee information. After the information on the file is read the hours variable has to be added up. I cannot figure out how to get the hours to add up though. This is what I have so far.

    while(inputFile.hasNext())
    {
    hours = inputFile.nextInt();
    rate = inputFile.nextDouble();
    name = inputFile.nextLine();
    System.out.print(name);
    System.out.print(rate);
    System.out.println(hours);
    }

    This whileloop reads all the data in the text document and and prints it out on the screen. But when I try to add a totalHours variable inside the loop it prints out on every iteration of the loop. When I try to add a totalHours variable outside the loop it says the hours variable has not yet been declared. So how would I get the program to add up all the hours then print the total only once? Please help!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by mhz041986 View Post
    After the information on the file is read the hours variable has to be added up.
    Java Code:
    hours = inputFile.nextInt();
    Where is the addition? You might need 2 variables. One to hold the hours of the current employee and one to hold the total. Add one to the other.

  3. #3
    mhz041986 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    i have a totalHours variable declared outside of the loop. I do not know where to add it at though, that is my problem. I need it to add the hours and print them on the screen only once.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    declare total
    loop {
        get hours
        add to total
    }
    display total

  5. #5
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    I think Junky (as usual is right on point)

    Java Code:
    while(inputFile.hasNext())
         {
    hours = inputFile.nextInt();
    totalHours = totalHour + hours;
          }
    So everytime your loop runs the value of hours changes depending on what has been read, this value is then stored to totalHours, the next value for hours is then added to totalHours

    Where you declare totalHours depends on what you want to use it for

Similar Threads

  1. Replies: 8
    Last Post: 01-19-2011, 08:54 AM
  2. Accessing session variable from Java Class
    By Rohit Pokhrel in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-08-2010, 04:07 PM
  3. Replies: 5
    Last Post: 10-11-2010, 01:16 AM
  4. Replies: 6
    Last Post: 03-31-2009, 04:27 AM
  5. Variable not initialized after while loop
    By ejs7597 in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 05:00 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
  •