Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sarrah gerard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();


 
sarrah gerard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sarrah, welcome to JavaRanch

Could you try to Use Code Tags in the future? Thanks.
 
sarrah gerard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic