Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes For-loop help initialising? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For-loop help initialising?" Watch "For-loop help initialising?" New topic
Author

For-loop help initialising?

Tom-Colin Kelly
Greenhorn

Joined: Dec 09, 2011
Posts: 27
Hello

I have some problems because just for some practice in getting better in Java I am practicing a for-loop. I am aiming to create a program that allows the user to type 10 numbers into the computer and calculates the total. however i have made the loop 10 times, and the userInput, however i am not sure how to sum this, i am currently trying to make a variable which will add all of the user inputs into.

Here is my code:



I am just looking for some help why the 'num + userInput;' doesnt work and how i can fix this, thanks.

Tom
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

Tom-Colin Kelly wrote:
I have some problems because just for some practice in getting better in Java I am practicing a for-loop. I am aiming to create a program that allows the user to type 10 numbers into the computer and calculates the total. however i have made the loop 10 times, and the userInput, however i am not sure how to sum this, i am currently trying to make a variable which will add all of the user inputs into.

Here is my code:



I am just looking for some help why the 'num + userInput;' doesnt work and how i can fix this, thanks.

Tom



Well, this is not a for-loop issue -- as that line won't have any effect regardless of having a for-loop.

Question? What do you think that line is supposed to do?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30382
    
150

You are REALLY close. Thanks for showing what you did so far.


This does add the two numbers. Then it throws away the result. You need an = on the line to save it.

num += userInput or num = num + userInput will save it to the num variable.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

What your code is doing in the state you posted it is this: each time you enter a loop, a new value (the one entered by user) is assigned to userInput at line 11. At line 12 you are basically doing nothing: you add userInput and num variable (which is not initialized in your code so it has the value of zero), and the result of that sum is not assigned to any variable so it's useless.

What you can do to have the result you want is the following:
  • You declare a variable, say sum before for loop, and initialize it to zero. This variable will be used to accumulate inputs gathered by user and calculate total sum of them.
  • Inside the for loop, read the variable userInput as you did at line 11; that's the current input from the user.
  • After that add the value entered by user to the variable sum (not the way you did it at line 12).


  • Once you get out of the loop, you can print the total sum of the numbers entered.

    Also, there probably isn't a very good reason why you make those variables static. userInput can be declared as variable local to the method you posted (which it is, indeed), and that num doesn't really have any significance so you can as well remove it.


    The quieter you are, the more you are able to hear.
    Tom-Colin Kelly
    Greenhorn

    Joined: Dec 09, 2011
    Posts: 27
    Henry Wong wrote:


    Well, this is not a for-loop issue -- as that line won't have any effect regardless of having a for-loop.

    Question? What do you think that line is supposed to do?

    Henry


    i know it isnt the loop, its an issue with the naming of 'num', but i am not sure how to fix tis?
    i thin this is supposed to as for the input 10 times and each time it adds what number they added into the variable 'num' so if my first number is 3 and second is 3 the num after that will be 6?

    Thanks for the input
    Tom-Colin Kelly
    Greenhorn

    Joined: Dec 09, 2011
    Posts: 27
    Jeanne Boyarsky wrote:You are REALLY close. Thanks for showing what you did so far.


    This does add the two numbers. Then it throws away the result. You need an = on the line to save it.

    num += userInput or num = num + userInput will save it to the num variable.


    Thank you it worked perfectly
    Ive done simething simmilar before but i froget i needed the = too!
    And thanks for explaining it!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38508
        
      23
    Please go back to your for loop and work out how often it will run. Be very careful.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: For-loop help initialising?