There are many ways to accomplish your goal. Here's one approach: Create an initially empty list (such as an ArrayList) that will be used to store the numbers entered by the user. As each number is entered, check it against those already in the list. If the number is not in the list, add it to the list. If the number is already in the list, do nothing. The length of the list will equal the number of unique numbers entered.
For my latest books on Java, including Introducing JavaFX 8 Programming, see HerbSchildt.com
Herb, sorry, but I really disagree with that. Whenever the word "unique" comes up, any Java developer should be thinking "ah, a Set". So let me suggest that you turn all numbers entered into java.lang.Integer objects that you put into a numbers Set (a HashSet will do fine). At the end, numbers.size() will tell you the number of distinct numbers entered.
- Peter [ August 22, 2004: Message edited by: Peter den Haan ]
Joined: Dec 10, 2001
I think, for anybody just starting to learn programming, writing a few of your own data structures (once) can be good exercise. It can strengthen basic programming skills, while creating a better understanding of how a particular data structure works, and how to use it.
After writing your own data structures, learn how to use the Collections API.
Joined: Oct 01, 2003
Dirk and Peter:
Yes, a Set is a good choice, as you indicate. I assumed (perhaps wrongly) that Qing was supposed to handle the "uniqueness" aspect manually as part of an exercise. (Perhaps to learn about seaching a list, how to implement simple data structures, etc.)
A Set, such as a HashSet, is useful in the problem you describe because it does not allow duplicate elements. This is why Dirk and Peter suggested it.
Joined: Aug 17, 2004
thank you all for help, but for me I haven't learn set (hashset)yet and many other useful tools. as beginner it is hard to understand so could please give me a little example to help for understanding.
Originally posted by Qing Tian: thank you all for help, but for me I haven't learn set (hashset)yet and many other useful tools. as beginner it is hard to understand so could please give me a little example to help for understanding.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus