# Thread: printing number of pos/neg numbers input?

1. Member
Join Date
Sep 2009
Posts
31
Rep Power
0

## printing number of pos/neg numbers input?

i need to write a code that when you input 5 numbers, it displays how many pos, neg and 0s were entered. i thought i was pretty close...but as you can see my output is not right. can someone please point me in the right direction from herE? thank you.

Java Code:
```import java.util.Scanner;
public class negposzerovals {

public static void main(String[] args) {

int n1;
int n2;
int n3;
int n4;
int n5;
int posnum;
int negnum;
int zeros;

posnum = 0;
negnum = 0;
zeros = 0;

Scanner input = new Scanner (System.in);

n1 = input.nextInt();
if (n1 > 0);
posnum++;
if (n1==0);
zeros++;
if (n1 < 0);
negnum++;

n2 = input.nextInt();
if (n2 > 0);
posnum++;
if (n2==0);
zeros++;
if (n2 < 0);
negnum++;

n3 = input.nextInt();
if (n3 > 0);
posnum++;
if (n3==0);
zeros++;
if (n3 < 0);
negnum++;

n4 = input.nextInt();
if (n4 > 0);
posnum++;
if (n4==0);
zeros++;
if (n4 < 0);
negnum++;

n5 = input.nextInt();
if (n5 > 0);
posnum++;
if (n5==0);
zeros++;
if (n5 < 0);
negnum++;

System.out.print("There were " + posnum + " positive numbers entered, " + negnum + "negative numbers entered, and " + zeros + "zeros entered.");

}

}```
Last edited by shroomiin; 09-25-2009 at 01:03 AM.

2. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
9
Use a counter? Just create a variable that is visible in the class itself, name it count or so, and set it's initial value to 0. Every time a number is inputted, increase the counter by one...

I don't really think this would solve your problem, and if it won't, you should rephrase your question, clearly.

~Matt

3. Member
Join Date
Sep 2009
Posts
31
Rep Power
0
yes sorry for the confusion. i am giving it a go right now and i have gotten it started but i know what i have right now will not work in the end. heres what i have:

import java.util.Scanner;
public class negposzerovals {

public static void main(String[] args) {

int n1;
int n2;
int n3;
int n4;
int n5;
int posnum;
int negnum;
int zeros;

Scanner input = new Scanner (System.in);

n1 = input.nextInt();
if (n1 > 0);
posnum = 1;
if (n1 < 0);
negnum = 1;

System.out.print("There were " + posnum + " positive numbers entered");

}

}

obviously i am still working on it and i would add more to the end, but as far as my variables and my math in the code go i think i need a little guidance. how would you go about fixing this?

4. Maybe look into using a for statement.

That way you can have one set of commands repeated x amount of times.
That would be the best way to get five inputs.

Also you forget to check if a number is a zero.

Maybe try drawing a flow chart of how the program should run, then try to implement it.

Note: Also, use the code tags next time you post your source code.

5. Member
Join Date
Sep 2009
Posts
31
Rep Power
0
i am only allowed to use if or while for this assignment, unfortunately.

6. Member
Join Date
Sep 2009
Posts
31
Rep Power
0
ALRIGHT. i thought i was going well until the code didnt work. this is what i have now. i think that i am close. any suggestions pleasE?
Java Code:
```import java.util.Scanner;
public class negposzerovals {

public static void main(String[] args) {

int n1;
int n2;
int n3;
int n4;
int n5;
int posnum;
int negnum;
int zeros;

posnum = 0;
negnum = 0;
zeros = 0;

Scanner input = new Scanner (System.in);

n1 = input.nextInt();
if (n1 > 0);
posnum++;
if (n1==0);
zeros++;
if (n1 < 0);
negnum++;

n2 = input.nextInt();
if (n2 > 0);
posnum++;
if (n2==0);
zeros++;
if (n2 < 0);
negnum++;

n3 = input.nextInt();
if (n3 > 0);
posnum++;
if (n3==0);
zeros++;
if (n3 < 0);
negnum++;

n4 = input.nextInt();
if (n4 > 0);
posnum++;
if (n4==0);
zeros++;
if (n4 < 0);
negnum++;

n5 = input.nextInt();
if (n5 > 0);
posnum++;
if (n5==0);
zeros++;
if (n5 < 0);
negnum++;

System.out.print("There were " + posnum + " positive numbers entered, " + negnum + "negative numbers entered, and " + zeros + "zeros entered.");

}

}```

7. Member
Join Date
Sep 2009
Posts
31
Rep Power
0
edited 1st post...can someone please assist?

8. Member
Join Date
Sep 2009
Posts
31
Rep Power
0
Well requirement was to create the program using only the topics we covered in the first chapter. in the first chapter it just covered the basics such as printing text and if statements or while statements.

the problem is: Write a program that inputs five numbers and determines and prints the number of positive, negative, and zero's entered. and then prints them.

i thought i was doing pretty good with it until the output said that all 3 values were 5...so i am somwhat lost on what to do about that. thanks again. i appreciate it.

9. If you can use a while, then use a while statement. You can make it do the same thing as a for statement

Java Code:
```int counter = 0;
while(counter < 5)
{
System.out.println(counter);
counter++;
}```

10. Member
Join Date
Feb 2009
Posts
92
Rep Power
0
Look at the syntax for "if" again. Your if statements don't do anything except make the comparison.

This is one of those problems that you can stare at till you're blind and not find the problem because you know what you mean and it seems so simple.

11. Originally Posted by rdtindsm
Look at the syntax for "if" again. Your if statements don't do anything except make the comparison.

This is one of those problems that you can stare at till you're blind and not find the problem because you know what you mean and it seems so simple.
Good pick-up. I think we'll just need to tell the OP.

To the original poster, the semicolon at the end of your if statement is short-circuiting it. It shouldn't be:

Java Code:
```if (blah);  // this semicolon is messing things up
myVar++;```
but rather,

Java Code:
```if (blah)  // semicolon is now gone.
myVar++;```
even better is to use curly braces:

Java Code:
```if (blah)  {
myVar++;
}```

12. Member
Join Date
Sep 2009
Posts
31
Rep Power
0
thanks so much everybody. after fixing my if syntax it works like a charm surprisingly.

#### Posting Permissions

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