# Sum positive numbers using 10 inputs from user

• 10-09-2010, 05:54 PM
pvictory1
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

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; System.out.println("Please enter 10 numbers, \n" + "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); } }```
• 10-09-2010, 06:11 PM
eRaaaa
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;
Code:

```                for (int w = 0; w < N; w++) {                         number = console.nextDouble();                         if (number > 0)                                 total += number;                 }```
• 10-09-2010, 06:28 PM
Fubarable
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:

Code:

```[cod&#101;]   // your code goes here   // notice how the top and bottom tags are different [/cod&#101;]```
Best of luck, and again, welcome!
• 10-09-2010, 06:33 PM
pvictory1
Thank you so much for your help, but how do I get the program only to accept 10 inputs.
• 10-09-2010, 06:37 PM
Fubarable
Quote:

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.
• 10-09-2010, 06:47 PM
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);

}
}
• 10-09-2010, 07:01 PM
Fubarable
Quote:

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);

}
}

• 10-09-2010, 07:06 PM
pvictory1
Quote:

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; System.out.println("Please enter 10 numbers, \n" + "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); } }```
• 10-09-2010, 07:11 PM
Fubarable
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!
• 10-09-2010, 07:19 PM
pvictory1
Quote:

Ok is this better?
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;                 System.out.println("Please enter 10 numbers, \n"                 + "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);         } }```
• 10-09-2010, 07:23 PM
eRaaaa
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.
• 10-09-2010, 07:29 PM
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:
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;       System.out.println("Please enter 10 numbers, \n"               + "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:
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;     System.out.println("Please enter 10 numbers, \n"         + "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);   } }```
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?
• 10-09-2010, 08:05 PM
pvictory1
Quote:

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:
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;       System.out.println("Please enter 10 numbers, \n"               + "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:
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;     System.out.println("Please enter 10 numbers, \n"         + "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);   } }```
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?

Quote:

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
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;       System.out.println("Please enter 10 numbers, \n"               + "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);   } }```
• 10-09-2010, 08:16 PM
Fubarable
Quote:

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?
• 10-09-2010, 08:40 PM
pvictory1
Quote:

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.
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;                 System.out.println("Please enter 10 numbers, \n"                 + "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);         } }```
• 10-10-2010, 01:30 AM
al_Marshy_1981
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.