This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
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
 
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.
 
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 )
 
Sheriff
Posts: 21452
95
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!