Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP 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  RSS feed

 
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.
 
Sheriff
Posts: 23451
46
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.
 
author
Marshal
Posts: 23435
138
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
Posts: 23451
46
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.
 
Bartender
Posts: 3185
34
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.
 
Marshal
Posts: 58339
178
  • 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
Marshal
Posts: 58339
178
  • 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
Boost this thread!