This is a code that I wrote and for some odd reason it doesn't count the number of inputted integers properly.

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{
  System.out.println("The number of integers is " + num);
}


}
}

If i were to enter lets say 9 numbers, it will say 2 numbers instead of 9. If i enter 25, it will say 500 instead of 25. The only time it works is if you enter a number like 0.

can someone help me with this?

Your question is not clear in your mind.

Use while loop and break it when your input==0.And take input from user within that loop,and check your input.If it is not 0 then increment the variable count.

Here you are not counting inegers at all,and just taking input once.

This maybe a ridiculous question but how would you do that within my code? I can see what happens with input==0 but where would num++ go?

Not num++....It's your input. use a seprate counter variable,where you want.
that is, when you input number which is not 0.Now you get it...right??
Dont forget to use a while loop as i said before.

Am I getting close to what you mean?

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();
int input = input++;
        if ( num == 0 ){
  System.out.println("No numbers were entered except 0");
}
else{
  System.out.println("The number of integers is " + input);
}
}
}

I have a feeling i did something horrificly wrong in that snippet of code...

while(num!=0)
{
count++;
//here again write code with to get input to `num` variable
}
System.out.print("Total Integer Numbers="+count);

no...You have gone too far.

  while(1){ //ask user and take input.
        //if it is 0 --> break;
        //else count++;
       }

Now I hope you'll come closer to what you want. :)

Ummmm... i am kind of confused between the two.

@dev90, why do you have a (1) next to the while loop? May you do that on my snippet, it doesn't make sense at some parts (my tired and crazy logical side is kicking in)

Hey, Sorry. Actually I want to edit my 1st line to while(true).
This is to make the loop infinite and we will break it when the no==0 using break;

Now you got why i used while(true).The Output will look like this:

...>java IntegerCount
Enter an integer, the input ends if it is 0: 11
Enter an integer, the input ends if it is 0: 22
Enter an integer, the input ends if it is 0: 33
Enter an integer, the input ends if it is 0: 0

No numbers were entered except 0

Total numbers entered are : 3

I hope the output is same you want.

@james, they are not exactly the same... the question began to change a bit on this thread, and so did the code.

Sorry, i won't "double post a thread" next time.

So here is what i have so far, am i using while(true) correctly? It says that is incorrect:

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

    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{
  System.out.println("The number of integers is " + num);
}
}
}
}

Your aim is to count the integers right? So why are you outputting num, the integer entered by the user?

You need to declare a counting variable. Let's say count. Now increment count if num!=0 in the while after the user has entered an integer. This is because we do not need to count 0 right? Since 0 is the terminator value.

Now in the else statement instead of num will be count.

Also I would not use while(true) for this type of question. You have to put a break somewhere for this. I would do the same as Learner010 wrote. Try that method. After you got the logic down then try doing what dev90 said.

Okay, so you mean like this:

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

    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{
  System.out.println("The number of integers is " + count++);
}
}
}
}

I think i did it wrong? also, where do i put a break in this?

try this:

import java.util.Scanner;
public class IntegerCount
{
public static void main(String[] args)
{
int num,count=0;
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer, the input ends if it is 0: ");
num = input.nextInt();
while(num!=0){
    count++;
    System.out.print("Enter an integer, the input ends if it is 0: ");
    num = input.nextInt();  
}
System.out.print("total integers="+count);
}
}

Hmmmm... that doesn't seem to work because it just repeats the string.

Try this way:

        int count =0;//counter variable
        int num = -1;//just a sentinal value
        while(num!=0)//stops the loop once input of 0
        {

            Scanner input = new Scanner(System.in);
            System.out.print("Enter an integer, the input ends if it is 0: ");
            num = input.nextInt();
            //if num!=0 then count++
            //if count == 0 then output "No numbers were entered except"
            //else output "The number of integers is " + count
        }

Hope that helps :)

@<M> just add count++ on line 17
and break on line 14.

@dev90,

Like this:

import java.util.Scanner;
public class IntegerCount
{
public static void main(String[] args)
{
int num,count=0;
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer, the input ends if it is 0: ");
num = input.nextInt();
while(num!=0){
    count++;
    System.out.print("Enter an integer, the input ends if it is 0: ");
    num = input.nextInt();  
break;
}
System.out.print("total integers="+count);
}
int count++;    
}

No. dev90 was referring to your code above 5 hours ago:

import java.util.Scanner;
public class IntegerCount
{
public static void main(String[] args)
{
 while(true){
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{
  System.out.println("The number of integers is " + count++);
}
}
}
}

Hmmm... i tried your code and it said: "cannot find symbol
System.out.println("The number of integers is " + count++);"

Replace this:

System.out.println("The number of integers is " + count++);"

with

System.out.println("The number of integers is " + count++);

@kal_crazy
why should not have break in else part because you pass true in while , which means it will repeat , and repeat and never stop . that's why i think of something like break to stop interation.

@<M> The code you have post after jamesCherill's post....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.