Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Joshua Emerson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please let me know how to find whether it is a numeral or a string and to filter them.
 
Henry Wong
author
Marshal
Pie
Posts: 20905
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have a List containing different types of values in the first place?
 
Joshua Emerson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic