Forums Register Login

Dealing with Array

+Pie Number of slices to send: Send
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:)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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

Normally trees don't drive trucks. Does this tiny ad have a license?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 882 times.
Similar Threads
wrapper class
Search Element in array Problem
sorting string with out using predefined java methods
Null pointer Exception
need help figurng out what is wrong with my A* search algorithm for an eight puzzle
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:32:10.