stuck on simple java program :(

Associate
Joined
1 May 2006
Posts
785
Location
UK
so I am trying to write a small, simple program to ask the user to input 5 integers; they can be either positive or negative.

after 5 have been entered I want to output the results as

"the sum of the positive integers you entered ="

and

"the sum of the negative integers you entered ="


so far I have this and am now at a loss :(

package week2;
import java.util.Scanner;


public class Week2 {


public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
System.out.print("Please enter first number, + or - : ");
int num1 = kybd.nextInt();

System.out.print("Please enter second number, + or - : ");
int num2 = kybd.nextInt();
System.out.print("Please enter third number, + or - : ");
int num3 = kybd.nextInt();
System.out.print("Please enter fourth number, + or - : ");
int num4 = kybd.nextInt();
System.out.print("Please enter fifth number, + or - : ");
int num5 = kybd.nextInt();

if (num1 > 0)
{pos++;}


else
{neg++;}




System.out.println("sum of positive numbers entered is :" + pos);
System.out.println("sum of negative numbers entered is :" + neg);



}

}


can anyone point me in the correct direction ?
 
Last edited:
Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
If you're using Java 8 summing a list is quite easy. Keep two lists posList and negList, depending on the input add it the appropriate list.

Once all input is entered you can do

Code:
int posSum = posList.stream().mapToInt(Integer::intValue).sum();
int negSum = negList.stream().mapToInt(Integer::intValue).sum();

If it's not java 8 then you would have to loop through the list adding to your sum as you go eg.

Code:
int posSum = 0;
for(Integer i: posList) {
    posSum += i;
}
 
Last edited:
Permabanned
Joined
9 Aug 2009
Posts
12,234
Location
UK
I think they're trying to each you to use a for loop.

for 1 to 5:
- ask user for a number
- determine if number is positive or negative and increment the corresponding counter

then print the two counts

for extra credit you could allow the user to enter the number of numbers they want to enter.

for extra credit you could keep track of the numbers entered and print them with the output, e.g. "you entered 3 positive numbers, they were 1, 2, and 3."

this will become a theme as you progress, it's worth taking some time to think about the problem before writing code. one way to do it is to write comments first like what I've written, then fill the code in afterwards.
 
Last edited:
Soldato
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
Code:
public class Test 
{
    public static void main(String[] args) 
    {
        Scanner kybd = new Scanner(System.in);
        
        int posInts = 0;
        int negInts = 0;
        
        int enteredNumber;
        
        for(int i = 0; i < 5; i++)
        {
            System.out.println("Please enter a +ve or -ve number:");
            enteredNumber = kybd.nextInt();
            if(enteredNumber > 0)
            {
                posInts = posInts + enteredNumber; 
            }
            else
            {
                negInts = negInts + enteredNumber;
            }
        }
        System.out.println("sum of positive numbers entered is :" + posInts);
        System.out.println("sum of negative numbers entered is :" + negInts);
    }
}
 
Back
Top Bottom