# Thread: how to ask the user to enter a sequence of numbers one at a time

1. Member
Join Date
Sep 2012
Posts
13
Rep Power
0

## how to ask the user to enter a sequence of numbers one at a time

Hi.
I have created this little program. It is the solution to exercise 6.17 of Deitel "how to program Java". The program asks the user to add a sequence of numbers and the program tests if it is odd or even...here is what I did. However, I don't know how to ask the user to enter more than one integers (one at a time) before I show the results:

//Exercise 6.17 Solution : EvenOdd.java

import java.util.Scanner;

public class EvenOdd
{
public void evenOrOdd()
{
Scanner input = new Scanner( System.in );

System.out.printf("%s\n%s\n",
"Please enter an integer and I will tell you if it is even or odd: ",
"Enter 0 to exit: ");

int number= input.nextInt();

while (number !=0)
{
if ( isEven( number ) )
System.out.printf( "%d is even\n", number );
else
System.out.printf( "%d is odd\n", number );

number= input.nextInt();
} // end while loop
} // end method evenOrOdd

// return true if number is even
public boolean isEven( int number )
{
return number % 2 == 0;
} // end method isEven
}//end of class EvenOdd

This program output for example is:

enter an integer: 4
4 is even
enter an integer: 5
5 is odd.

how can you program it showing the following:

enter an integer: 4
enter an integer: 5
4 is even
5 is odd.

2. Member
Join Date
Sep 2012
Posts
17
Rep Power
0

## Re: how to ask the user to enter a sequence of numbers one at a time

import java.util.Scanner;

public class EvenOdd
{
public static final int NUMBER = 5;
static int[] array = new int[NUMBER];
public static void main(String[] args) {
Scanner input = new Scanner( System.in );

System.out.printf("%s\n%s\n",
"Please enter an integer and I will tell you if it is even or odd: ",
"Enter 0 to exit: ");
for (int i = 0; i < NUMBER; i++) {
System.out.println("Please enter number " + (i+1) + " : ");
array[i]= input.nextInt();
}

for (int i = 0; i < NUMBER; i++) {
isEven( array[i] );
}
}

public static void isEven( int number )
{
if(number % 2 == 0)
System.out.printf( "%d is even\n", number );
else System.out.printf( "%d is odd\n", number );
}
}

this is the code i write. one thing you need to know is that, you have to initialize the number of the integers you want to enter to the system, like the definition of NUMBER which is static in my code. otherwise, it is impossible to realize what you want, personally opinion.
hope it helps.

3. Member
Join Date
Sep 2012
Posts
13
Rep Power
0

## Re: how to ask the user to enter a sequence of numbers one at a time

my original post was so clear ...
I also gave an example.

P.S. I am not in Arrays yet....

4. Moderator
Join Date
Jul 2010
Location
California
Posts
1,638
Rep Power
13

## Re: how to ask the user to enter a sequence of numbers one at a time

thinkinjava24's post is what is called spoon-feeding - evidently thinkinjava24 does not know what this is, but for his benefit I'd recommend reading The Problem with Spoon-feeding.

Back to the problem at hand

how can you program it showing the following:
Write out how you would do this on paper - do you know before hand how many integers a user want's to enter? If no, then you must prompt the user to enter this value if you wish for that output.

#### Posting Permissions

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