programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Help with Loops

Ranch Hand
Posts: 53
1
• Number of slices to send:
Optional 'thank-you' note:
Write a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered. When the loop finishes, display the accumulated total.

That's the question I've been trying to solve for the past 3 days and I'm running out of time.  Does anyone know how I can write this?

Greenhorn
Posts: 14
2
• Number of slices to send:
Optional 'thank-you' note:
What have you come up with so far?

Jacob Sousie
Ranch Hand
Posts: 53
1
• Number of slices to send:
Optional 'thank-you' note:

Chris Sadowski wrote:What have you come up with so far?

I don't think I'm doing this right...

Greenhorn
Posts: 14
2
• Number of slices to send:
Optional 'thank-you' note:
Ok, its a start But in the question you are asked to take in the the input 10 times, and every time add it together.

First step to changing your code would be to make sure you ask for the input 10 times. Right now you only ask for it once, and then loop over 10 times. With a minior change to your code you can ask for the input 10 times. This should get you started! After that you have to make sure to keep track of the sum like the question asked.

Hope this helps.

Jacob Sousie
Ranch Hand
Posts: 53
1
• Number of slices to send:
Optional 'thank-you' note:

Chris Sadowski wrote:Ok, its a start But in the question you are asked to take in the the input 10 times, and every time add it together.

First step to changing your code would be to make sure you ask for the input 10 times. Right now you only ask for it once, and then loop over 10 times. With a minior change to your code you can ask for the input 10 times. This should get you started! After that you have to make sure to keep track of the sum like the question asked.

[code]

public static void main (String [] args) throws IOException{

System.out.print("Enter 10 numbers: ");
int i;
String input;
Scanner keyboard = new Scanner (System.in);

for (i = 0; i < 10; i++) {
System.out.println("Enter next number:");
input = keyboard.nextLine(); //just like you did before, but now in a loop
System.out.println("You entered: "+input); //'input' contains your number

}

keyboard.close(); //dont forget to close
}
[\code]

Hope this helps.

Thanks!   now it runs 10 times,  But How do I make it add up all 10 numbers?  I don't know where they are stored or how to use the + feature to add them all up.

Greenhorn
Posts: 14
2
• Number of slices to send:
Optional 'thank-you' note:
This should point you to what you need for this exercise:

http://www.wikihow.com/Find-the-Sum-of-Two-Numbers-in-Java

Jacob Sousie
Ranch Hand
Posts: 53
1
• Number of slices to send:
Optional 'thank-you' note:

Chris Sadowski wrote:This should point you to what you need for this exercise:

http://www.wikihow.com/Find-the-Sum-of-Two-Numbers-in-Java

Yeah I totally get that.  But in a loop how does it know what loop number its on?  without breaking it?

lets say I make a loop that runs twice.
and the first loop I enter 5
and the second loop I enter 5

what are those variables called?

maybe input + input?  or would that just make it enter 55?

Greenhorn
Posts: 14
2
• 1
• Number of slices to send:
Optional 'thank-you' note:
Ok, that's a good question.

I think the biggest thing you're missing is that you defined the 'input' variable as String. This will treat the '5' in your example as just a character. You wanted to treat it as a number - because then you can properly add them. First step would be changing 'String input' to 'int input' as int (short for integer) represents a numerical value.  When you do this, however, the line in your code 'keyboard.nextLine()' will not work, as it will expect to be saved into a String. So you need to get a next int from your 'keyboard' , instead of the nextLine() which is a String. Please take a closer look at the link I sent, and its all there ;)

Jacob Sousie
Ranch Hand
Posts: 53
1
• Number of slices to send:
Optional 'thank-you' note:

Chris Sadowski wrote:Ok, that's a good question.

I think the biggest thing you're missing is that you defined the 'input' variable as String. This will treat the '5' in your example as just a character. You wanted to treat it as a number - because then you can properly add them. First step would be changing 'String input' to 'int input' as int (short for integer) represents a numerical value.  When you do this, however, the line in your code 'keyboard.nextLine()' will not work, as it will expect to be saved into a String. So you need to get a next int from your 'keyboard' , instead of the nextLine() which is a String. Please take a closer look at the link I sent, and its all there ;)

YES YOU'RE RIGHT!  Thank you so much!

It worked!   thanks again so much.  I understand it now!    Is there any way you could look at my other posts?  I have a few other questions xD

Jacob Sousie
Ranch Hand
Posts: 53
1
• Number of slices to send:
Optional 'thank-you' note:

Chris Sadowski wrote:Ok, that's a good question.

I think the biggest thing you're missing is that you defined the 'input' variable as String. This will treat the '5' in your example as just a character. You wanted to treat it as a number - because then you can properly add them. First step would be changing 'String input' to 'int input' as int (short for integer) represents a numerical value.  When you do this, however, the line in your code 'keyboard.nextLine()' will not work, as it will expect to be saved into a String. So you need to get a next int from your 'keyboard' , instead of the nextLine() which is a String. Please take a closer look at the link I sent, and its all there ;)

Wait...  I did something wrong...  I'll see If I can figure it out.

Jacob Sousie
Ranch Hand
Posts: 53
1
• Number of slices to send:
Optional 'thank-you' note:

Chris Sadowski wrote:Ok, that's a good question.

I think the biggest thing you're missing is that you defined the 'input' variable as String. This will treat the '5' in your example as just a character. You wanted to treat it as a number - because then you can properly add them. First step would be changing 'String input' to 'int input' as int (short for integer) represents a numerical value.  When you do this, however, the line in your code 'keyboard.nextLine()' will not work, as it will expect to be saved into a String. So you need to get a next int from your 'keyboard' , instead of the nextLine() which is a String. Please take a closer look at the link I sent, and its all there ;)

Great... what did I do wrong now... lol

Greenhorn
Posts: 14
2
• Number of slices to send:
Optional 'thank-you' note:
Much closer!

But right now lets say in the very first loop, you enter '5', so you make your input have a 5 in it, then when you do

sum = input+input+input....

You're effectively doing:

sum = 5+5+5+5...

Which is not exactly what you want.

In a loop you only want to add the 'input' once. But you need something to keep track of what the sum is so far.

if you start with sum=0; before you start your loop, and then think about what would happen if instead of
sum = input+input+input

you tried:
sum = sum + input;

Jacob Sousie
Ranch Hand
Posts: 53
1
• Number of slices to send:
Optional 'thank-you' note:

Chris Sadowski wrote:Much closer!

But right now lets say in the very first loop, you enter '5', so you make your input have a 5 in it, then when you do

sum = input+input+input....

You're effectively doing:

sum = 5+5+5+5...

Which is not exactly what you want.

In a loop you only want to add the 'input' once. But you need something to keep track of what the sum is so far.

if you start with sum=0; before you start your loop, and then think about what would happen if instead of
sum = input+input+input

you tried:
sum = sum + input;

So like this?

Greenhorn
Posts: 14
2
• 1
• Number of slices to send:
Optional 'thank-you' note:
System.out.println(sum + input);  will only print whats curretly saved in 'sum'. In your save you only saved sum = 0; You need add to the sum every time what was in the sum before, plus the new input value.

all you're missing is sum = sum + input;

Then you dont need to print (sum + input), you can just print the sum.

Jacob Sousie
Ranch Hand
Posts: 53
1
• 1
• Number of slices to send:
Optional 'thank-you' note:

Chris Sadowski wrote:System.out.println(sum + input);  will only print whats curretly saved in 'sum'. In your save you only saved sum = 0; You need add to the sum every time what was in the sum before, plus the new input value.

all you're missing is sum = sum + input;

Then you dont need to print (sum + input), you can just print the sum.

AHHHH I SEE.  I see what you mean!  Now it will keep track of the numbers and keep building!   Thank you!

Greenhorn
Posts: 14
2
• 1
• Number of slices to send:
Optional 'thank-you' note:
I'm just realizing

sum = sum + input;

may seem confusing to you. In programming, what this means is that whatever is on the right side of the '=' will be saved to the variable on the left.

This means if you start with sum =0, then make input = 5 (from the command line), when you do
sum = sum + input;, you will save 0+5 (so, 5) to the variable sum. In the next loop, when your input will be lets say 3, when you do the same sum = sum + input, sum was already 5, so you will save 5+3 to the 'sum' (so, 8).

But don't listen to my explanation directly. There are tons and tons of resources that can make it much clearer online. I strongly recommend you check them out, it's gonna save you a lot of time with your assignments in the future Good luck!

Bartender
Posts: 3323
86
• Number of slices to send:
Optional 'thank-you' note:

This line should be split into 2 lines: doing more than one thing on a line just makes the code that bit more difficult to read and understand. It would be far better to write:

Tony Docherty
Bartender
Posts: 3323
86
• 1
• Number of slices to send:
Optional 'thank-you' note:
Chris: excellent help on this thread - have a cow.

Marshal
Posts: 79039
375
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch

Just one thing: never close a Scanner pointing to System.in. You must close Scanners pointing to files, etc, but not System.in.

 He was giving me directions and I was powerless to resist. I cannot resist this tiny ad: a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com