programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Using percent Modulus to calculate total number of even numbers from user input

Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:
Ok my assignment for class is to use the percent mod to calculate the total number of all the numbers and the total number of even numbers only. This should be done by using the user's input of their number.
the % "mod" operator
Input:
The first line of input will be a random list of integers the user will put it.
Output:
Return the total integers in the list and count of even integers.
Sample:
Input   Output
3 4 10 12 1 0 6 78 7 #total: 9
#even: 6
...............................................................
this is what I got so far

sandy skiathitis
Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:

sandy skiathitis wrote:Ok my assignment for class is to use the percent mod to calculate the total number of all the numbers and the total number of even numbers only. This should be done by using the user's input of their number.
the % "mod" operator
Input:
The first line of input will be a random list of integers the user will put it.
Output:
Return the total integers in the list and count of even integers.
Sample:
Input   Output
3 4 10 12 1 0 6 78 7 #total: 9
#even: 6
...............................................................
this is what I got so far

Saloon Keeper
Posts: 15449
363
• Number of slices to send:
Optional 'thank-you' note:
Your for loop appears broken. Take a step back and write down in plain English what the program should do:

1) Read one line from user input
2) While the line has another integer:
2.1) Increase total count of integers
2.2) If the integer is even:
2.2.1) Increase total count of even integers
3) Print total count of integers and total count of even integers

Now solve each step separately. Test your program in between solving steps, and make sure it does what you expect it to do. For instance, print the line after step 1) to make sure it's the same as you've entered.

You seem to have trouble with step 2). Look in the Scanner class for a very useful method.

sandy skiathitis
Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:

Stephan van Hulst wrote:Your for loop appears broken. Take a step back and write down in plain English what the program should do:

1) Read one line from user input
2) While the line has another integer:
2.1) Increase total count of integers
2.2) If the integer is even:
2.2.1) Increase total count of even integers
3) Print total count of integers and total count of even integers

Now solve each step separately. Test your program in between solving steps, and make sure it does what you expect it to do. For instance, print the line after step 1) to make sure it's the same as you've entered.

You seem to have trouble with step 2). Look in the Scanner class for a very useful method.

so i would have to use a while statement then an if statement but how to I implement the remainder or percent modulud operator? also would i have to do something specefically so that the program will be able to read more than one input the user puts in??? sorry this is my first programming class and first project of the semester...

Stephan van Hulst
Saloon Keeper
Posts: 15449
363
• Number of slices to send:
Optional 'thank-you' note:
Here's a skeleton:

sandy skiathitis
Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:

Stephan van Hulst wrote:Here's a skeleton:

I am so confused....the outline helped but I just dont know enough computer language to put it together... I am on the right track? but I have to use percent mod..or the remainder of mod...

sandy skiathitis
Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:

sandy skiathitis wrote:

Stephan van Hulst wrote:Here's a skeleton:

I am so confused....the outline helped but I just dont know enough computer language to put it together... I am on the right track? but I have to use percent mod..or the remainder of mod...

this is what I got..I am soooo close...how can I do the total number count...I got lost..can you help???

Stephan van Hulst
Saloon Keeper
Posts: 15449
363
• Number of slices to send:
Optional 'thank-you' note:
Sandy, Scanner has a very useful method called hasNextInt().

Here's an almost complete solution: