Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes An array list contains Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "An array list contains "abc", 3, "xyz", 7: How can i filter the numerals and display" Watch "An array list contains "abc", 3, "xyz", 7: How can i filter the numerals and display" New topic
Author

An array list contains "abc", 3, "xyz", 7: How can i filter the numerals and display

Joshua Emerson
Greenhorn

Joined: Apr 21, 2008
Posts: 19
An array list contains "abc", 3, "xyz", 7, ...... How can i filter the numerals and display the output?

I tried with ArrayList.contains(), but not able to get the expected output. Please can some one help me on this.

Thank you.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Pseudo-code:



I assume you can write the for-loop to examine each element in the list? If you have a question about how to find whether an element is a numeral, or some other question about this, don't hesitate to post it.
Joshua Emerson
Greenhorn

Joined: Apr 21, 2008
Posts: 19
Please let me know how to find whether it is a numeral or a string and to filter them.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Joshua Emerson wrote:Please let me know how to find whether it is a numeral or a string and to filter them.


One option is to use the 'instanceof" operator to check if the object is an instance of either the String or Number class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Henry Wong wrote:
Joshua Emerson wrote:Please let me know how to find whether it is a numeral or a string and to filter them.


One option is to use the 'instanceof" operator to check if the object is an instance of either the String or Number class.

Henry


True, assuming that the objects in the list are of different types. But we haven't been told that, we're left to guess.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3097
    
  33

Joshua Emerson wrote:...How can i filter the numerals and display the output? ...


As it is to filter and display Numerals, we can as well do an instanceof on Number.


Mohamed Sanaulla | My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Why do you have a List containing different types of values in the first place?
Joshua Emerson
Greenhorn

Joined: Apr 21, 2008
Posts: 19
Campbell,

I'm sure we shall filter the String or Integer while defining the Array list itself. But i need the logic in one of the scenarios.

Thank you all for your guidance and helping me in resolving my big problem.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
It sounds as if you might have a design problem with a List of alternating Strings and ints. It suggests maybe you need a Map instead, or an object which encapsulates the two values.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: An array list contains "abc", 3, "xyz", 7: How can i filter the numerals and display