File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Counting inputs and scanner class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Counting inputs and scanner class" Watch "Counting inputs and scanner class" New topic
Author

Counting inputs and scanner class

James Farber
Greenhorn

Joined: Feb 01, 2007
Posts: 6
How do you count the amount of inputs using the Scanner class?
Say for instance I want the user to enter an even, and only an even amount of numbers

"Enter an even amount of numbers :"

5 12 7 2

after hitting return, how can I store these into one variable?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
For a problem like this, I recommend you start with a smaller problem and then build on your solution For example:

1. Write a program that reads just one number from the list using a Scanner and prints it out.

2. Add a loop to make the Scanner read repeatedly. Is there a way to make the Scanner stop once there are no more numbers to read?

3. Add an int variable "count". Every time you go through the loop, add one to the count. After you're done with the loop, print the count.

If you get stuck on one part, show us what you've done so far and we can offer more suggestions.


"I'm not back." - Bill Harding, Twister
K. Tsang
Ranch Hand

Joined: Sep 13, 2007
Posts: 1219

This looks harder than it sounds!! Yet I see there are many ways to do it.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Counting inputs and scanner class
 
Similar Threads
List of numbers input from scanner into an arraylist
User addition result Help needed.
how to accept i/p's from user
simple array loop
Largest/Greatest of n numbers