Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-25-2008, 07:14 AM
Member
 
Join Date: Jun 2008
Posts: 8
Mathan is on a distinguished road
Map concept in JAVA
Hi,

I am new to JAVA. I have some problem for using map concept.

I am calling getPas method & returning like this.
Map<String,Object> param = getPas(string xyz, String[] Name);
String[] config = (String[]) param.get(Name);

using the Name argument i am fetching the values from Map and casting to String[]. So if the param.get() returns more than one value( like ABC, DEF, KLM, OPQ...) it is working fine. But it return single value( like ABC), it is not storing to config variable, instead throwing error ( type casting error)

Can any one give me the better solution?

Note : i have one more function :
Map<String,Object> param = getPas(string xyz, String[] Name);
String config = (String) param.get(Name);

In this function will not work for String[] means more than one return value( like ABC, DEF, KLM, OPQ...). Thats why i created one new function( the top function)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-25-2008, 07:49 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
What your getPas() do? And what error throw by your code?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-25-2008, 08:13 AM
Member
 
Join Date: Jun 2008
Posts: 8
Mathan is on a distinguished road
getPas() method will read from local cache. It is throwing Casting Exception. Do you need any more information?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-25-2008, 08:27 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 476
fishtoprecords is on a distinguished road
Make it simpiler until you get it working.
Use the standard get() function, which takes one parameter, the key

Once that works, you can wrap it in other code.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-25-2008, 08:32 AM
Member
 
Join Date: Jun 2008
Posts: 8
Mathan is on a distinguished road
yes, here the key value is "Name" as already i given the same. Still i am getting problem
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-25-2008, 09:08 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
As fishtoprecords says, in simpler way is that working?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-26-2008, 12:55 PM
Member
 
Join Date: Jun 2008
Posts: 8
Mathan is on a distinguished road
i cannot understand
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-26-2008, 01:32 PM
Member
 
Join Date: May 2008
Posts: 24
daGame is an unknown quantity at this point
Quote:
Originally Posted by Mathan View Post
i cannot understand
Test the method using normal Map without using the generics and get it

using the normal get() method. First get this running and then move on to

use generics, generics can get tricky as i've found out, you have to use the

casts properly.

"Use the standard get() function, which takes one parameter, the key" - fishtoprecords

Read the api for maps. It has a function which takes only the key parameter

WTF i cant post any links....this is terrible

Last edited by daGame : 06-26-2008 at 01:36 PM.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-26-2008, 01:42 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
generics cause damages on you for latest versions. So better to move with latest version.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-26-2008, 01:56 PM
Member
 
Join Date: May 2008
Posts: 24
daGame is an unknown quantity at this point
Quote:
Originally Posted by Eranga View Post
generics cause damages on you for latest versions. So better to move with latest version.
Generics are the latest version. Generics are supported from jdk 1.5 onwards

Are we missing something mod?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 06-26-2008, 02:10 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Oops, a big mistake. That word should be change.

Quote:
generics cause damages on you for early versions. So better to move with latest version.
Really sorry about my mistake.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 06-26-2008, 05:05 PM
Member
 
Join Date: Jun 2008
Posts: 8
Mathan is on a distinguished road
Hi all,

i am getting this exception for above query.
"java.lang.ClassCastException: java.lang.String"

See the code :

String[] configParam = null;

Map<String, Object> paramMap = getNetworkPropertiesFromPas(packageName,
new String[] {parameterName});
if (paramMap != null) {
try{
Set keys = paramMap.keySet(); // The set of keys in the map.
Iterator keyIter = keys.iterator();
while (keyIter.hasNext()) {
String key = (String)keyIter.next(); // Get the next key.
configParam = (String[])paramMap.get(key); // Get the value for that key.

for( int f=0; f < configParam.length; f++ ) {
System.out.println( ".....................Value[" + f + "] is = (" + configParam[f] + ")" );
}
}

}
catch(Exception e){
System.out.println("Exception e is = " + e);
}
}





Here : configParam = (String[])paramMap.get(key);

If the configParam value is more than one value means (like configParam[0]=abc, configParam[1]=def, configParam[2]=ijk) its working fine.
But the value is single means (like configParam[0]=abc) throwing exception like "java.lang.ClassCastException: java.lang.String"


Can any one give me the quick solution?
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 06-26-2008, 05:19 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 476
fishtoprecords is on a distinguished road
Code:
Map<String, Object> paramMap =.... (snip) configParam = (String[])paramMap.get(key); // Get the value for that key.
You should chante the code to look like

Object oFromMap = paramMap.get(key);
System.out.println(oFromMap.getClass().getName();

so you can see what you are getting. Don't cast blindly.

And if you really are placing an array of String in the map, declare it that way.

Map<String, String[]t> paramMap =....
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 06-26-2008, 05:46 PM
Member
 
Join Date: Jun 2008
Posts: 8
Mathan is on a distinguished road
Hi,
I tried "Map<String, String[]t> paramMap =...." this...

But i got the below syntax error.
Type mismatch: cannot convert from HashMap<String,Object> to Map<String,String[]>
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 06-26-2008, 06:20 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
String[] is not absolutely equal to Object.....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 06-27-2008, 06:17 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by sukatoa View Post
String[] is not absolutely equal to Object.....
Yep, this is what you have going wrong here.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 06-27-2008, 07:16 AM
Member
 
Join Date: Jun 2008
Posts: 8
Mathan is on a distinguished road
That is Mr.Fishtoprecords suggestion . so i tried that way also.... not helping. Can any one tell me the good way?

And one thing i can not change to String[] in the Map argument <String, String[]>. because already lot of other functions using only object <String, Object>. So give me better solution ASAP
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 06-27-2008, 07:24 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 476
fishtoprecords is on a distinguished road
I won't give you a solution. I will help.

Did you do the System.out.println that I suggested upthread? What did it tell you? How do you use the information it gave you?
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 06-27-2008, 07:59 AM
Member
 
Join Date: Jun 2008
Posts: 8
Mathan is on a distinguished road
Yes this is not a solution. thanks for your helping. yes i did System.out. That is not printing anything.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 06-27-2008, 08:02 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 476
fishtoprecords is on a distinguished road
Didn't print anything? I've never heard of that.
Try changing it to "hello world" and make sure that its getting executed.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Which concept used in java for monitoring the printer kalinga2k4 Networking 1 06-14-2008 03:25 AM
mail concept indirani New To Java 3 04-16-2008 03:30 PM
mail concept thamizhisai Advanced Java 4 04-11-2008 09:19 AM
What is RMI concept in Spring Framework Java Tip Spring Framework 0 04-02-2008 12:36 PM
easy way to study the java springs concept kumar84 New To Java 1 07-17-2007 05:53 PM


All times are GMT +3. The time now is 12:40 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org