• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to count total number without same number

 
Qing Tian
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that such a data structure is called a set.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic