Results 1 to 13 of 13
 09242009, 11:55 PM #1Member
 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); System.out.print("Please enter first number:"); n1 = input.nextInt(); if (n1 > 0); posnum++; if (n1==0); zeros++; if (n1 < 0); negnum++; System.out.print("Please enter second number:"); n2 = input.nextInt(); if (n2 > 0); posnum++; if (n2==0); zeros++; if (n2 < 0); negnum++; System.out.print("Please enter third number:"); n3 = input.nextInt(); if (n3 > 0); posnum++; if (n3==0); zeros++; if (n3 < 0); negnum++; System.out.print("Please enter fourth number:"); n4 = input.nextInt(); if (n4 > 0); posnum++; if (n4==0); zeros++; if (n4 < 0); negnum++; System.out.print("Please enter fifth number:"); 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; 09252009 at 02:03 AM.
 09252009, 12:04 AM #2Senior Member
 Join Date
 Aug 2008
 Posts
 384
 Rep Power
 7
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.
~MattI die a little on the inside...
Every time I get shot.
 09252009, 12:10 AM #3Member
 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);
System.out.print("Please enter first number:");
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?
 09252009, 12:33 AM #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.
 09252009, 01:02 AM #5Member
 Join Date
 Sep 2009
 Posts
 31
 Rep Power
 0
i am only allowed to use if or while for this assignment, unfortunately.
 09252009, 01:13 AM #6Member
 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); System.out.print("Please enter first number:"); n1 = input.nextInt(); if (n1 > 0); posnum++; if (n1==0); zeros++; if (n1 < 0); negnum++; System.out.print("Please enter second number:"); n2 = input.nextInt(); if (n2 > 0); posnum++; if (n2==0); zeros++; if (n2 < 0); negnum++; System.out.print("Please enter third number:"); n3 = input.nextInt(); if (n3 > 0); posnum++; if (n3==0); zeros++; if (n3 < 0); negnum++; System.out.print("Please enter fourth number:"); n4 = input.nextInt(); if (n4 > 0); posnum++; if (n4==0); zeros++; if (n4 < 0); negnum++; System.out.print("Please enter fifth number:"); 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."); } }
 09252009, 02:04 AM #7Member
 Join Date
 Sep 2009
 Posts
 31
 Rep Power
 0
edited 1st post...can someone please assist?

Can you post the exact assignment requirements?
Last edited by Fubarable; 09252009 at 02:12 AM.
 09252009, 02:31 AM #9Member
 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.
 09252009, 02:43 AM #10
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++; }
 09252009, 02:45 AM #11Member
 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.

Good pickup. I think we'll just need to tell the OP.
To the original poster, the semicolon at the end of your if statement is shortcircuiting it. It shouldn't be:
Java Code:if (blah); // this semicolon is messing things up myVar++;
Java Code:if (blah) // semicolon is now gone. myVar++;
Java Code:if (blah) { myVar++; }
 09252009, 03:15 AM #13Member
 Join Date
 Sep 2009
 Posts
 31
 Rep Power
 0
Similar Threads

Log4j not printing out file and line number
By devin in forum Java ServletReplies: 2Last Post: 03052009, 04:16 PM 
Printing ranges of numbers using if/else statements
By russbuss106 in forum New To JavaReplies: 8Last Post: 03052009, 06:03 AM 
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10152008, 12:46 AM 
trying to add up random numbers into one number
By pjr5043 in forum New To JavaReplies: 4Last Post: 09152008, 03:20 PM 
Printing Fibonacci Numbers
By Java Tip in forum java.langReplies: 0Last Post: 04092008, 07:43 PM
Bookmarks