# Thread: Sum positive numbers using 10 inputs from user

1. Member Join Date
Oct 2010
Posts
7
Rep Power
0

## Sum positive numbers using 10 inputs from user

Hi

I'm a little confused about this, I can't seem to get this to work corretly. What I'm trying to accomplish is I'm taking ten positive double number inputs and only printing out the positive ones plus the total. The problem is I don't know how to only have it except 10 inputs and also it's not totaling the inputs correctlly. Please help. Thank you

Java Code:
```import java.util.*;

public class Sum
{
static Scanner console = new Scanner(System.in);
static final int N = 10;

public static void main(String[]args)
{
double number;
int total = 0;

+ "adds the positive numbers, and displays the totals.");

System.out.print("Please enter " + N + " numbers; seperate each with a space: ");

for (int w = 1; w <= N; w++)
{
number = console.nextDouble();

if (number > 0)

total += number;w++;
{
continue;
}
}
System.out.println("The total of the positive numbers is: " + total);

}
}```
Last edited by Fubarable; 10-09-2010 at 06:24 PM. Reason: Moderator Edit: Code tags added -- but the code wasn't formatted to begin with -- D'oh!  Reply With Quote

2. Senior Member  Join Date
Oct 2010
Location
Germany
Posts
785
Rep Power
11

## you raise "w" twice. Remove the w++ after total += number;!
Your for-loop raise the w automatically after each iteration (see: The for Statement (The Java Tutorials > Learning the Java Language > Language Basics))
So you can input 10 double values now.
The second problem (" it's not totaling the inputs correctlly.") is, that you declare total as an Integer. Use double total = 0; instead of int total = 0;
Java Code:
```		for (int w = 0; w < N; w++) {
number = console.nextDouble();
if (number > 0)
total += number;
}```  Reply With Quote

3. ## Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

Java Code:
```[cod&#101;]
// notice how the top and bottom tags are different
[/cod&#101;]```
Best of luck, and again, welcome!  Reply With Quote

4. Member Join Date
Oct 2010
Posts
7
Rep Power
0

## Thank you so much for your help, but how do I get the program only to accept 10 inputs.  Reply With Quote

5. ##  Originally Posted by pvictory1 Thank you so much for your help, but how do I get the program only to accept 10 inputs.
Accept input within a for loop and make sure that the loop iterates only 10 times.  Reply With Quote

6. Member Join Date
Oct 2010
Posts
7
Rep Power
0

## Hi again,
Ok, if I change my for loop as show in the program it still doesn't stop at 10. I must be missing the boat on this one.

import java.util.*;

public class Sum
{
static Scanner console = new Scanner(System.in);
static final int N = 10;

public static void main(String[]args)
{
double number;
double total = 0;

+ "adds the positive numbers, and displays the totals.");

System.out.print("Please enter " + N + " numbers; seperate each with a space: ");

for (int w = 1; w <= 10;w++)
{
number = console.nextDouble();

if (number > 0)

total += number;
{
continue;
}
}
System.out.println("The total of the positive numbers is: " + total);

}
}  Reply With Quote

7. ##  Originally Posted by pvictory1 Hi again,
Ok, if I change my for loop as show in the program it still doesn't stop at 10. I must be missing the boat on this one.

import java.util.*;

public class Sum
{
static Scanner console = new Scanner(System.in);
static final int N = 10;

public static void main(String[]args)
{
double number;
double total = 0;

+ "adds the positive numbers, and displays the totals.");

System.out.print("Please enter " + N + " numbers; seperate each with a space: ");

for (int w = 1; w <= 10;w++)
{
number = console.nextDouble();

if (number > 0)

total += number;
{
continue;
}
}
System.out.println("The total of the positive numbers is: " + total);

}
}  Reply With Quote

8. Member Join Date
Oct 2010
Posts
7
Rep Power
0

## Java Code:
```import java.util.*;

public class Sum
{
static Scanner console = new Scanner(System.in);
static final int N = 10;

public static void main(String[]args)
{
double number;
double total = 0;

+ "adds the positive numbers, and displays the totals.");

System.out.print("Please enter " + N + " numbers; seperate each with a space: ");

for (int w = 1; w <= 10;w++)
{
number = console.nextDouble();

if (number > 0)

total += number;
{
continue;
}
}
System.out.println("The total of the positive numbers is: " + total);

}
}```  Reply With Quote

9. ## Thanks for the code tags, but you still may want to indent your code according to Java convention (that is what is meant above by posting formatted code). Either that or hopefully someone won't mind reading it all left justified and will help you. Best of luck!  Reply With Quote

10. Member Join Date
Oct 2010
Posts
7
Rep Power
0

## Ok is this better?
Java Code:
```import java.util.*;

public class Sum
{
static Scanner console = new Scanner(System.in);
static final int N = 10;

public static void main(String[]args)
{
double number;
double total = 0;

+ "adds the positive numbers, and displays the totals.");

System.out.print("Please enter " + N + " numbers; seperate each with a space: ");

for (int w = 1; w <= 10;w++)
{
number = console.nextDouble();

if (number > 0)

total += number;
{
continue;
}
}
System.out.println("The total of the positive numbers is: " + total);

}
}```  Reply With Quote

11. Senior Member  Join Date
Oct 2010
Location
Germany
Posts
785
Rep Power
11

## i think you dont understand your own program :)

"System.out.print("Please enter " + N + " numbers; seperate each with a space: "); "

that is not that what your program do... you must input a double value, press enter, and so on...

after 10 numbers, the loop ends and the sum is printed.  Reply With Quote

12. ## Almost, but your curly braces are all over the place. This may seem trivial to you, but trust me, if you do this properly (or use a decent IDE that does this for you), you will be able to spot errors and problems much more quickly and be able to understand code that you've written a month ago much more quickly. There are two main styles of brace placement, that I call C style:
Java Code:
```import java.util.*;

public class Sum {
static Scanner console = new Scanner(System.in);
static final int N = 10;

public static void main(String[] args) {
double number;
double total = 0;

+ "adds the positive numbers, and displays the totals.");

System.out.print("Please enter " + N + " numbers; seperate each with a space: ");

for (int w = 1; w <= 10; w++) {
number = console.nextDouble();

if (number > 0)

total += number;
{
continue;
}
}
System.out.println("The total of the positive numbers is: " + total);

}
}```
or Pascal style:
Java Code:
```package linkAutoIt.v001;

import java.util.*;

public class Sum
{
static Scanner console = new Scanner(System.in);
static final int N = 10;

public static void main(String[] args)
{
double number;
double total = 0;

+ "adds the positive numbers, and displays the totals.");

+ " numbers; seperate each with a space: ");

for (int w = 1; w <= 10; w++)
{
number = console.nextDouble();

if (number > 0)

total += number;
{
continue;
}
}
System.out.println("The total of the positive numbers is: " + total);

}
}```
Either will do for general use, but you'll want to stick with one or the other -- usually the one your teacher or team leader requests.

Looking at your code with indentations OK, it seems like it should work, that if you enter more than 10 numbers, it will only look at the first 10. I do have to wonder what the "continue" statement is doing in your code. Why do you have it in there?  Reply With Quote

13. Member Join Date
Oct 2010
Posts
7
Rep Power
0

##  Originally Posted by Fubarable Almost, but your curly braces are all over the place. This may seem trivial to you, but trust me, if you do this properly (or use a decent IDE that does this for you), you will be able to spot errors and problems much more quickly and be able to understand code that you've written a month ago much more quickly. There are two main styles of brace placement, that I call C style:
Java Code:
```import java.util.*;

public class Sum {
static Scanner console = new Scanner(System.in);
static final int N = 10;

public static void main(String[] args) {
double number;
double total = 0;

+ "adds the positive numbers, and displays the totals.");

System.out.print("Please enter " + N + " numbers; seperate each with a space: ");

for (int w = 1; w <= 10; w++) {
number = console.nextDouble();

if (number > 0)

total += number;
{
continue;
}
}
System.out.println("The total of the positive numbers is: " + total);

}
}```
or Pascal style:
Java Code:
```package linkAutoIt.v001;

import java.util.*;

public class Sum
{
static Scanner console = new Scanner(System.in);
static final int N = 10;

public static void main(String[] args)
{
double number;
double total = 0;

+ "adds the positive numbers, and displays the totals.");

+ " numbers; seperate each with a space: ");

for (int w = 1; w <= 10; w++)
{
number = console.nextDouble();

if (number > 0)

total += number;
{
continue;
}
}
System.out.println("The total of the positive numbers is: " + total);

}
}```
Either will do for general use, but you'll want to stick with one or the other -- usually the one your teacher or team leader requests.

Looking at your code with indentations OK, it seems like it should work, that if you enter more than 10 numbers, it will only look at the first 10. I do have to wonder what the "continue" statement is doing in your code. Why do you have it in there?
Hi again, Here is what is asked of me. Code a program called SumPositive to enter 10 numbers and only total those that are positive. prompt the user to enter the numbers (double) and print out the total of only the positive ones
Java Code:
```import java.util.*;

public class Sum {
static Scanner console = new Scanner(System.in);
static final int N = 10;

public static void main(String[] args) {
double number;
double total = 0;

+ "adds the positive numbers, and displays the totals.");

System.out.print("Please enter " + N + " numbers; seperate each with a space: ");

for (int w = 1; w <= 10; w++) {
number = console.nextDouble();

if (number > 0)

total += number;
{
continue;
}
}
System.out.println("The total of the positive numbers is: " + total);

}
}```  Reply With Quote

14. ## Hi again, Here is what is asked of me. Code a program called SumPositive to enter 10 numbers and only total those that are positive. prompt the user to enter the numbers (double) and print out the total of only the positive ones
OK, but again please describe as precisely as can be what's the current problem and again, please answer the qeustion: why does your program have "continue" in it? What purpose does it serve?  Reply With Quote

15. Member Join Date
Oct 2010
Posts
7
Rep Power
0

## The current problem is I need to only accept 10 double numbers and only 10 double numbers and its allowing me to enter in more then 10 numbers. The contiune is just one of the options we could have used but I see that I don't need to use it.
Java Code:
```import java.util.*;

public class Sum {

static Scanner console = new Scanner(System.in);
static final int N = 10;

public static void main(String[]args){
double number;
double total = 0;

+ "adds the positive numbers, and displays the totals.");

System.out.print("Please enter " + N + " numbers; seperate each with a space: ");

for (int w = 1; w <= 10;w++) {

number = console.nextDouble();

if (number > 0)

total += number; }

System.out.println("The total of the positive numbers is: " + total);

}
}```  Reply With Quote

16. Senior Member Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11

## nope you do not need continue, and the next question you should be asking yourself is how do I processs a positive number and a negative one. think if statement.  Reply With Quote

#### Posting Permissions

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