Okay, so this is a similar error to the code before.

So I created this:

import java.util.Scanner;
public class IntegerCount
{
public static void main(String[] args)
{

    Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer, the input ends if it is 0: ");
int num = input.nextInt();  

        if (num == 0)
            {
                System.out.println("No numbers were entered except 0");
            } 
        else if (num != 0)
            {
                System.out.println("The number of integers is " + num);
            }
        else
            {
                System.out.print(num);
            }

}
}

But i begin to notice that when I run the program, it collects random numbers. For example, the professor expects the number 9, my program pulls out a 2? I don't see where i am making that mistake?

I am not really sure actually where i am getting the wrong input/outputs...

The program automattically inputs a number (9 in this case) but my program returns a 2? I am not sure why....

I'm a bit puzzled about this part:

if (num == 0)
            {
                System.out.println("No numbers were entered except 0");
            } 
        else if (num != 0)
            {
                System.out.println("The number of integers is " + num);
            }
        else
            {
                System.out.print(num);
            }

for which value of num do you think it'll reach the last print statement?

so, you enter a 9 and it returns 2, did I get that right?

Edited 2 Years Ago by stultuske

so, you enter a 9 and it returns 2, did I get that right?

That is correct except the program the professor uses enters 9 and my little project returns a 2.

if (num == 0)
{
System.out.println("No numbers were entered except 0");
}
else if (num != 0)
{
System.out.println("The number of integers is " + num);
}
else
{
System.out.print(num);
}

Yeah, i am not exactly sure what I was doing there... i think i confused myself on the if...else statement? Do you have a way for me to write it more efficiently or at least in a way where i can solve this problem???

I ran that code. when I entered 9, the output was 9. no problem there.

the above code can easily be rewritten as:

if ( num == 0 ){
  System.out.println("No numbers were entered except 0.");
}
else{
  System.out.println("The number of integers is " + num);
}

the only reason I see to add the last else if is if you addd a test on whether it was a negative number or not.
are you sure you saved/recompiled your code before trying to run it?

the only reason I see to add the last else if is if you addd a test on whether it was a negative number or not. are you sure you saved/recompiled your code before trying to run it?

Yes, I have recompiled the code before running it... i guess the professor's program is messed up... :D

I am going to leave this thread open and close tomorrow morning (if there is a response from the teacher claiming he messed up)

But thanks!

Edited 2 Years Ago by <M/>

YOu didn't post the requirements, but this line:
System.out.println("The number of integers is " + num);
implies that num should be a count of how many integers were entered, but what you are printing is just the latest integer. You don't count them.

@James, I see what you mean but if I am not counting them, what would I be doing? What should be displayed is just all the numbers the users have submitted until they hit 0...

Edited 2 Years Ago by <M/>

You are asking me what the requirements for this exercise are? I have no idea, but presumably your teacher gave you them. It would help if you shared them with us.

Oh sorry about that, i thought i shared enough that was needed.

Well here are the instructions:

Read in integer numbers, one integer number per line, stop reading when you input 0, you will output the number integers read. You will first prompt the user with the string "Enter an integer, the input ends if it is 0: ".

Sample Run 1:

Enter an integer, the input ends if it is 0: 2
1
-2346
8534
9
0
The number of integers is 5

Sample Run 2:

Enter an integer, the input ends if it is 0: 0
You only submitted 0

Consistency Notes Repeated...
Output lines are always newline terminated. The string for only 0 entered is "No numbers were entered except 0\n";

I think i got most of it, i hope...

Edited 2 Years Ago by <M/>

ah, ok, that's a different matter.
first, you declare a counter, before you loop.
I'll give you the pseudo code here:

set counter to 0
read number

while ( number != 0 )
add 1 to counter
print number
read number
end-while

if counter is 0
print only 0 entered
else
counter numbers entered
end-if

@stultuske, hmmmmmm if that is what i am supposed to do, i am going to expirement with that for a little bit :D

Thanks and goodnight (until morning :D)

This article has been dead for over six months. Start a new discussion instead.