Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: acessing variabloe outside loop

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default acessing variabloe outside 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
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: acessing variabloe outside loop

    If I understand correctly, you want a integer value from a String, from which you can add up. Use either the wrapper class for the number primitive you need, for example Integer.parseInt, to get that number value
    String value = "123";
    int intValue = 0;
    try{
        intValue = Integer.parseInt(value);
    }catch(NumberFormatException e){
        e.printStackTrace();
    }

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: acessing variabloe outside loop

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/41792-accessing-variable-loop.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Duplicate post!


  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: acessing variabloe outside loop

    @copeg: Don't nextInt() extracts integer from the string and casts it to integer datatype?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: acessing variabloe outside loop

    Quote Originally Posted by Mr.777 View Post
    @copeg: Don't nextInt() extracts integer from the string and casts it to integer datatype?
    Thanks for the correction

Similar Threads

  1. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  2. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  3. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  4. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 02:10 PM
  5. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM