First off, im a beginner at Java. Im taking a class where we are currently learning about If-statements and While-statements, and theyre tripping me up a little bit. Any help would be appreciated.

Here is the desired output for what were doing...

Enter 10 whole numbers: 0 2 -10 21 0 8 -5 5 -3 -2
Sum of the positive numbers: 36
Sum of the negative numbers: -20
Sum of all the numbers: 16

Probably simple stuff for you guys, but heres whats causing me problems...We need to input the "10 whole numbers" after running the program, but I dont know how I can put just 10 numbers and have the program go to the next step, which is finding the sum.

Currently, there is no limit to the numbers I can put in there. I think I have a decent understanding of if statements, so I believe I can figure out the rest once I can just get how to put in just 10 numbers and have the program continue.

Thanks in an advance to anyone who can help me out a little bit.

• --first, you didn't mention how the numbers will be inputted into the program.. You could use GUI to accomplish this similar with Visual Basics Dialogs Box, Input Boxes etc.. or you will input through the console/command line (similar to C scanf).. but in java inputting through the console is trickier than using Java's GUI components..

i found this great post on how to accept input through the console:

-- the one who posted this is really good (i also don't know this..)

well hope this helps.. good luck..

start of the program..

import java.io.*;

public class inputTenNumbers{

public static void main(String [] args) throws IOException
{
int counter=0;
int sumOfAllInput=0, sumOfPositive=0, sumOfNegative=0;

while(counter<10){

//start of input code
System.out.println("Please Enter a Number (No: " + (counter+1) + "): ");
int fromConsoleInput = Integer.parseInt(stringInt);
// the variable frmConsoleInput hold the integer value of input
// which is converted to integer because the actual input from
// the console is actually string/characters
//end of input code

counter++;

sumOfAllInput = sumOfAllInput + fromConsoleInput;

if(fromConsoleInput > 0) //sum of positive numbers
sumOfPositive = sumOfPositive + fromConsoleInput;
else //sum of negative numbers
sumOfNegative = sumOfNegative + fromConsoleInput;

}

System.out.println("Sum of all positive numbers: " + sumOfPositive);
System.out.println("Sum of all negative numbers: " + sumOfNegative);
System.out.println("Sum of all numbers: " + sumOfAllInput);

}
}
• Thanks a lot, I appreciate the quick response.

Like I said, im about as much of a beginner as it gets. I use BlueJ, since that is what we use in class. The numbers are supposed to be inputted all at once on the same line, then the program will get the sums following the 10th number.

I notice you did it a little differently, but it helped me enough to finish the assignment. I had problems figuring out what exactly the while loop did with the 'counter'. Once I understood how to separate each number that I input the rest was cake.

However, one more question regarding this assignment. What if I wanted to modify the program to also find averages of positive, negative and all the numbers? How would that work with the if and else statements?

Thanks again, Im sure Ill have more questions about future assignments that Ill have to post here • glad I could help.. don't worry I'm also new to Java and it's nice that I learn something new from someone..

how to get the average..

new variables
- posCtr = 0; //counter on how many positive numbers entered
- negCtr = 0; // negative

if(fromConsoleInput > 0){ //sum of positive numbers
sumOfPositive = sumOfPositive + fromConsoleInput;
[color=Green]posCtr++;[/color]
}else{ //sum of negative numbers
sumOfNegative = sumOfNegative + fromConsoleInput;
[color=Green]negCtr++;[/color]
}
...
...
at the end of the loop then you can divide the total of the positive Numbers to the number of positive numbers entered (using the variable posCtr), same with the negative numbers and also with all the numbers use the variable holding the total of all the numbers and divide by ten..

ex: System.out.println("Average of Positive Numbers: " sumAllPositive / posCtr);

but can you tell me how did you accept 10 inputs in one line? so that I have something new to learn.. :-)

and by the way.. i'm not an math expert.. but how would you handle an input of zero.. would it fall in the positive or negative?? he he.. you should revise the if condition depending on how you would handle zero..

until next time..
• Thanks again. I used the Scanner method for the input of the numbers since thats what I know. It took me a little bit to figure out how to put all the numbers on one line, but heres the entire code from the program...As for the 0's, I figure those dont have any effect on the sum or average.

import java.util.*;
public class test7{

public static void main(String [] args)
{
int counter=0, numbers;
int sumAll=0, sumPos=0, sumNeg=0;
Scanner keyIn = new Scanner (System.in);

//start of input code
System.out.println("Enter 10 whole numbers: ");
while(counter<10){
numbers = keyIn.nextInt();

counter++;
sumAll = sumAll + numbers;

if(numbers > 0)
sumPos = sumPos + numbers;

else
sumNeg = sumNeg + numbers;

}

System.out.println("Sum of the positive numbers: " + sumPos);
System.out.println("Sum of the negative numbers: " + sumNeg);
System.out.println("Sum of all the numbers: " + sumAll);

}
}
• Thanks for the info.. I think its kinda advance for a beginner to learn something from Java's Data Collection objects.. I must say, you will be a good programmer someday... :-) , and by the way what is BlueJ? Is it a Java-IDE? I use eclipse.. thanks again..
• Ive never used eclipse, or any other Java program for that matter. BlueJ is a beginners program that is free(I dont know if others are) that we use in class. From what ive heard, BlueJ is a very good program to learn Java, but lacks the more advanced features that expert programmers could use. One thing I really dont like about Java is it doesnt compile in real time. You have to do the work, then compile it. For people like myself who do a lot of trial and error that gets annoying.
• Thanks for the information about BlueJ.. I should check it out.. you should try also eclipse.. It's kinda for advance programmers but I guess its not that difficult to learn compared to other Java IDE out there.. You'll get used to the compile procedure for Java.. again I advice you to check eclipse (eclipse.org) it's a good ide since it similar to VB's Visual Studio and gives good details on errors..
• yea when I first started learning java i started with BlueJ. Its great because you can see visually what is going on. Eclipse is what I use now and I love it as well.
• hello friends
why is the output of System.out.println(010); is 8 not 10

• what is the difference between int and INTEGER.

• edited June 2015

Use array to store 10 integers.

Example:
int a[]=new int; // array declaration
a[]={0, 2 ,-10, 21, 0, 8 ,-5, 5, -3, -2}
--After that use if else as follows:

for(int i=0;i<a.length;i++)
{
if( a[i]>=0)
{
sum_pos+=a[i]; // for adding +ve nos
}
else if(a[i]<0)
{
sum_neg+=a[i]; // for adding -ve nos
}
}

System.out.println("Sum of the positive numbers:"+sum_pos);
System.out.println("Sum of the negative numbers:"+sum_neg);
System.out.println("Sum of all the numbers:"+(sum_pos+sum_neg);

For more Details Refer following link:

http://java.meritcampus.com/t/32/Array---declaration--creation?tc=mm233