File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Problem in Creating HashMap object then display in sorted and unsorted order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in Creating HashMap object then display in sorted and unsorted order" Watch "Problem in Creating HashMap object then display in sorted and unsorted order" New topic
Author

Problem in Creating HashMap object then display in sorted and unsorted order

sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Hi All,
I'm really confused in that error
run:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at myhashmap.MyHashMap.main(MyHashMap.java:44)
Java Result: 1


Create your own NetBeans project named as MyHashMap

Create your own HashMap object

Add the following objects to the newly created HashMap object.

2 String objects with keys "1st", "2nd"

2 MyOwnClass objects with keys "3rd", "4th"

1 Integer ojbects with keys "5th"

Display them in both unsorted and sorted order

I tried to write this;
________________________________________________

_____________________________________________________________

__________________________________________
There are lots of reference as you see, Please help to arrenge my code
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
The problem is that you put in mixed type objects in the HashMap (such as String, Integer and MyOwnClass). Then when you retreive them you cast to String. This give you the ClassCastException.
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
This is the problem

String value =(String) m.getValue();

when you getValue() from the map, it can be String object, Integer object or MyOwnClass object, so you can't simply cast everything to String.


Try this

String value = m.getValue().toString();



SCJP 6, SCWCD 5, SCBCD 5
sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Embla Tingeling wrote:The problem is that you put in mixed type objects in the HashMap (such as String, Integer and MyOwnClass). Then when you retreive them you cast to String. This give you the ClassCastException.

Thanks Embla,
I add like this, after your arrangement

__________________________________
now the error turns in to
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - java.util.Comparator is abstract; cannot be instantiated
at myhashmap.MyHashMap.main(MyHashMap.java:26)
Java Result: 1

I think ,i am placed to wrong line. but, i don't know the correct way
sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Lee Kian Giap wrote:This is the problem

String value =(String) m.getValue();

when you getValue() from the map, it can be String object, Integer object or MyOwnClass object, so you can't simply cast everything to String.


Try this

String value = m.getValue().toString();




wooow you're right. now it runs
run:
UnSorted order is : {2nd=Passion, 1sth=Java, 5th=9, 3rd=aa, 4th=bb}
Sorted map {1sth=Java, 2nd=Passion, 3rd=aa, 4th=bb, 5th=9}

Thanks thanks a lot, with my Regards Lee Kian Giap )
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Sarrah, welcome to JavaRanch

Could you try to Use Code Tags in the future? Thanks.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sarrah gerard
Greenhorn

Joined: Nov 06, 2009
Posts: 18
Rob Prime wrote:Sarrah, welcome to JavaRanch

Could you try to Use Code Tags in the future? Thanks.


hahah ok ,i did,sorry
thanks Rob Prime
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in Creating HashMap object then display in sorted and unsorted order