aspose file tools*
The moose likes Beginning Java and the fly likes How to count total number without same number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to count total number without same number" Watch "How to count total number without same number" New topic
Author

How to count total number without same number

Qing Tian
Greenhorn

Joined: Aug 17, 2004
Posts: 10
Hi, could please teach me how to count total number without same number.

eg:
use loop had entered number is: 1
entered number is: 2
entered number is: 3
entered number is: 3

total entered number is 3 enter and not 4.

thanks a lot!
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Qing:

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 my Java Programming Cookbook, see HerbSchildt.com
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Note that such a data structure is called a set.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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 ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
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.)


Qing:

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.
Qing Tian
Greenhorn

Joined: Aug 17, 2004
Posts: 10
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.

thanks again for help me!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


Take a look at the Collections tutorial: http://java.sun.com/docs/books/tutorial/collections/


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
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
David J. Eck's Introduction to Programming Using Java includes an easy-to-understand introduction to data structures. I enjoyed reading it, when I first started to learn about them.
Qing Tian
Greenhorn

Joined: Aug 17, 2004
Posts: 10
Thank you all for help!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to count total number without same number