wood burning stoves 2.0*
The moose likes Java in General and the fly likes Dealing with Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dealing with Array" Watch "Dealing with Array" New topic
Author

Dealing with Array

Aggarwal Arpit
Ranch Hand

Joined: Mar 04, 2012
Posts: 86
Hi I have two arrays:
One Object array that I achieved after converting TreeSet and One String Array, In my selectedArray[] data is{"A","B","C"}



Now what I want to extract the data from my members[] which is not present in my selectedArray[] and store it in one ArrayList.
What I am doing is:


I am not getting correct output.
Please help me to solve this issue
Thanks in advance
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Why convert Set to Array when you can use contains() method over your set to check if the value in it exists? Your code is rather complicated for such a trivial task. And those labeled breaks should not be used unless necessary, and this certainly is not the situation for it.
Now what I want to extract the data from my members[] which is not present in my selectedArray[] and store it in one ArrayList.


When you iterate over your members array, check for each value if it is present in your Set, using method I mentioned at the beginning. If not, add it to your array list, otherwise do nothing. With a few lines of code you can solve your problem using this approach.


The quieter you are, the more you are able to hear.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11257
    
  16

Aggarwal Arpit wrote:I am not getting correct output.

It would help us greatly if you told us:
A) What output you are actually getting
B) What you think the correct output actually IS.

Otherwise, we can only guess what you are thinking or what you want.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Aggarwal Arpit
Ranch Hand

Joined: Mar 04, 2012
Posts: 86
Hi Kemal,
I tried same thins with Set but actually the problem is I am getting the data in Set after reading Excel sheet using poi.
So when I am compiling with code:


I am getting exception as
Exception in thread "main" java.lang.ClassCastException: org.apache.poi.hssf.usermodel.HSSFRichTextString cannot be cast to java.lang.String
at java.lang.String.compareTo(Unknown Source)
at java.util.TreeMap.getEntry(Unknown Source)
at java.util.TreeMap.containsKey(Unknown Source)
at java.util.TreeSet.contains(Unknown Source)
at com.test.ReadExcelFile.showExelData(ReadExcelFile.java:154)
at com.test.ReadExcelFile.main(ReadExcelFile.java:121)



So I thought of this idea to convert into Object Array and then do something to find odd one out
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Arpit, with the sample code that you have provided, it wouldnt be easy to figure out where and why the error is occuring. Create a simple, short program containing the part which is throwing the exception and post it here.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aggarwal Arpit wrote:Hi Kemal,
I tried same thins with Set but actually the problem is I am getting the data in Set after reading Excel sheet using poi.
So when I am compiling with code:


I am getting exception as
Exception in thread "main" java.lang.ClassCastException: org.apache.poi.hssf.usermodel.HSSFRichTextString cannot be cast to java.lang.String
at java.lang.String.compareTo(Unknown Source)
at java.util.TreeMap.getEntry(Unknown Source)
at java.util.TreeMap.containsKey(Unknown Source)
at java.util.TreeSet.contains(Unknown Source)
at com.test.ReadExcelFile.showExelData(ReadExcelFile.java:154)
at com.test.ReadExcelFile.main(ReadExcelFile.java:121)




So you have an HSSFRichTextString, and you're indirectly trying to cast it to a java.langString, by way of calling selectedData.contains(String), where the elements of selectedData are apparently HSSFRichTextStrings. Without going into the details of why you got that specific error, do you see why it doesn't make sense to look for one type of object in a TreeMap full of a completely different and unrelated type of object?

So I thought of this idea to convert into Object Array and then do something to find odd one out


That won't do anything. You still have all the same types of objects. Using an array or reference of a different type doesn't change the object in any way.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Praveen Kumar M K wrote:Arpit, with the sample code that you have provided, it wouldnt be easy to figure out where and why the error is occuring. Create a simple, short program containing the part which is throwing the exception and post it here.


Yes, an SSCCE(⇐click) would make this easier--one with just enough code to reproduce the problem, and nothing that's not directly required to achieve that end.
Aggarwal Arpit
Ranch Hand

Joined: Mar 04, 2012
Posts: 86
Jeff, Thanks for your valuable reply
Now my code is working.
I changed HSSFRichTextString to


and then I user removeAll function of ArrayList to remove data pressent in Set

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dealing with Array