File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dealing with Array

 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic