# Odd and Even Numbers

• 04-13-2012, 01:07 PM
pinkdreammsss
Odd and Even Numbers
Ive faced a problem here ,
user enters some numbers , programs shows how many of them are odd and how many are even.
here is what i have so far :

Code:

``` import java.util.Scanner; public class OddEven {     public static void main (String [] args){         int evenCount=0;int oddCount=0;         Scanner sc=new Scanner(System.in);         System.out.print("enter 10 numbers : ");               int input=sc.nextInt();       for(int i=1;i<input;i++){                           if (i%input==0){                 evenCount++;                         }             else {             oddCount++;             }                             }                            System.out.print("u have entered "+evenCount+" even numbers");                     }                           }```
each time i run the program (testing only even numbers) , i get 0. is there something wrong with my counter?
• 04-13-2012, 01:18 PM
Norm
Re: Odd and Even Numbers
Where do you change the value of the the variable: input inside of the loop?

Rethink your logic to get a new input to test.

How many numbers were you able to enter for the program to read when you executed the program?
This should have told you there was a logic problem.
• 04-13-2012, 01:35 PM
pinkdreammsss
Re: Odd and Even Numbers
Quote:

Originally Posted by Norm
Where do you change the value of the the variable: input inside of the loop?

Rethink your logic to get a new input to test.

How many numbers were you able to enter for the program to read when you executed the program?
This should have told you there was a logic problem.

lets say im trying to enter following numbers : 10 20 25 23 44 8 3 7 39 56 . without having to ask user to enter each number separately ,ive modified the code into this :

Code:

``` public class OddEven {     public static void main (String [] args){         int evenCount=0;int oddCount=0;         Scanner sc=new Scanner(System.in);         System.out.print("enter 10 numbers : ");               int input=sc.nextInt();       for(int i=1;i<50;i++){                             if (i%input==0){                 evenCount++;                         }             else {             oddCount++;             }                             }                              System.out.print("u have entered "+evenCount+" even numbers");                     }                           }```
but the output is not the exact count of even numbers.
• 04-13-2012, 01:42 PM
Norm
Re: Odd and Even Numbers
Where do you change the value of the variable: input inside of the loop?

Add a println statement inside of the loop immediately after the for statement (line 9) that prints out the value of the input variable so you can see what the computer sees when it executes the code.
• 04-13-2012, 02:14 PM
JosAH
Re: Odd and Even Numbers
A question for you: why do you loop fifty times? And why do you only read a number once? I don't understand your program logic and I think it is completely wrong.

kind regards,

Jos
• 04-13-2012, 03:11 PM
pinkdreammsss
Re: Odd and Even Numbers
Quote:

Originally Posted by JosAH
A question for you: why do you loop fifty times? And why do you only read a number once? I don't understand your program logic and I think it is completely wrong.

kind regards,

Jos

for the loop, i know the problem is the logic , my loop should generate a range of numbers, say from 1 to 100, so :if x%rangeOfnumbers=0 then count++; but the loop im using is not doing so.

also, user enters all the numbers at once so program wouldnt have to prompt for user input multiple times. e.g: 2 5 7 9 10 38. notice the space between each number.
• 04-13-2012, 03:14 PM
Sierra
Re: Odd and Even Numbers
That won't work... you need to split your input first for having separate numbers... try e.g. the split() function of the String class and loop through the resulting array of single numbers the user entered. ;)
Like that you try this: if ( 1 % "1 7 9 8" == 0)... 1 dividefd by a generic String won't work very well...
• 04-13-2012, 03:36 PM
pinkdreammsss
Re: Odd and Even Numbers
Quote:

Originally Posted by Sierra
That won't work... you need to split your input first for having separate numbers... try e.g. the split() function of the String class and loop through the resulting array of single numbers the user entered. ;)
Like that you try this: if ( 1 % "1 7 9 8" == 0)... 1 dividefd by a generic String won't work very well...

thanks for the tip , tho im trying to do this without using arrays.
• 04-13-2012, 03:43 PM
Norm
Re: Odd and Even Numbers
The call to nextInt() should work with all the numbers entered on one line. However if you do not call nextInt inside the loop you will not get the next number.
• 04-13-2012, 03:51 PM
Sierra
Re: Odd and Even Numbers
Sorry, my fault, Norm, you are right... you just need it inside the loop. :)
• 04-13-2012, 03:58 PM
pinkdreammsss
Re: Odd and Even Numbers
Quote:

Originally Posted by Norm
The call to nextInt() should work with all the numbers entered on one line. However if you do not call nextInt inside the loop you will not get the next number.

ive found a sample for calling nextInt() in the loop, but it's array , and i dont want an array.

Code:

```for (int i = 0; i < input ; i++ )     {         numbers[i] = input.nextInt();```
any idea how can i do this without using array?
• 04-13-2012, 04:06 PM
Norm
Re: Odd and Even Numbers
You don't need an array. Just move the assignment to input inside the loop. Use the hasNext() method to detect the end of input data.
• 04-14-2012, 04:36 AM
pinkdreammsss
Re: Odd and Even Numbers
Quote:

Originally Posted by Norm
You don't need an array. Just move the assignment to input inside the loop. Use the hasNext() method to detect the end of input data.

is this what u meant? gives me the error incompatible types.

Code:

```for(int i=0;i<100;i++){         int input=sc.hasNext();                  if (i%input==0){                 evenCount++;                         }```
• 04-14-2012, 12:20 PM
Norm
Re: Odd and Even Numbers
You need to read the API doc for the Scanner class for the nextInt() and for the hasNext() methods.
They return different values and are used for different purposes.

I said: Use the hasNext() method to detect the end of input data.
Not to read values from the user.
• 04-14-2012, 01:18 PM
pinkdreammsss
Re: Odd and Even Numbers
Quote:

Originally Posted by Norm
I said: Use the hasNext() method to detect the end of input data.
Not to read values from the user.

how am i suppose to detect the end of input data? any example?
• 04-14-2012, 01:26 PM
Norm
Re: Odd and Even Numbers
Did you look at the API doc for the hasNext() or hasNextInt() methods? The return a boolean that can be tested. Might be a problem with blocking to wait for input.

pseudo code
begin loop
is there more input - if not exit the loop
process the input
end loop

You might want to use a "sentinel" value the user types in to tell you that there is no more input.

Or read in the whole line of numbers with nextLine() and work through them either using another instance of Scanner.
Here's a sample:
Code:

```      Scanner scnr = new Scanner("1 2  3 4 5\n 6 8 9 0");       while(scnr.hasNextInt()) {         System.out.print(scnr.nextInt() + " ");  // 1 2 3 4 5 6 8 9 0       }```
• 04-19-2012, 05:57 AM
pinkdreammsss
Re: Odd and Even Numbers
i figured out the problem with my program,
i had to move
Code:

`int input=sc.nextInt();`
in a for loop so it'll keep count of numbers entered by user ,
e.g:
Code:

```for (int i=0;i<10;i++){ // the loop keeps count of entered numbers, int input=sc.nextInt();// here after each time user enters a numbers , loop count 1 , until 10 numbers are entered.  }```
the rest is up to if statement that calculates how many odd n even numbers are there. here's the final program:
Code:

```import java.util.Scanner; public class oddeven {     public static void main (String [] args){         int evenCount=0;int oddCount=0;         Scanner sc=new Scanner(System.in);         System.out.print("enter 10 numbers : ");                   for(int i=0;i<10;i++){         int input=sc.nextInt();                              if (input%2==0){                 evenCount++;                         }             else             oddCount++;                                               }                     System.out.print("u have entered "+evenCount+" even numbers and "+oddCount+" odd numbers");                     }                           }```