This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Using percent Modulus to calculate total number of even numbers from user input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using percent Modulus to calculate total number of even numbers from user input" Watch "Using percent Modulus to calculate total number of even numbers from user input" New topic
Author

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

sandy skiathitis
Greenhorn

Joined: Apr 22, 2011
Posts: 18
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

Joined: Apr 22, 2011
Posts: 18
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




so I redid it ummmmm here is the second version...please help i am getting more lost...


Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3579
    
  14

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

Joined: Apr 22, 2011
Posts: 18
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
Bartender

Joined: Sep 20, 2010
Posts: 3579
    
  14

Here's a skeleton:
sandy skiathitis
Greenhorn

Joined: Apr 22, 2011
Posts: 18
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

Joined: Apr 22, 2011
Posts: 18
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
Bartender

Joined: Sep 20, 2010
Posts: 3579
    
  14

Sandy, Scanner has a very useful method called hasNextInt().

Here's an almost complete solution:
 
jQuery in Action, 2nd edition
 
subject: Using percent Modulus to calculate total number of even numbers from user input
 
Similar Threads
I need help refining some code
Binary to Decimal conversion
User input into array
Netbeans and Ctrl + z (end of file)
Counting in Array List/ Standard deviation