Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to count total number without same number

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that such a data structure is called a set.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for help!
 
Beware the other head of science - it bites! Nibble on this message:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic