1

i am able to print my Out-put in this format System.out.println(map.get("email"));//this is printing fine but i am unable to print same value after assigning it into a String variable. i tried: String email=(String) map.get("email"); System.out.println("Email--"+email);//But this is not printing
How can i convert map values into string? Please help me.

My full code:

String url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token="
              + authResponse.accessToken; 
            final StringBuffer r = new StringBuffer(); 
            final URL u = new URL(url);
            final URLConnection uc = u.openConnection();
            final int end = 1000;
            InputStreamReader isr = null;
            BufferedReader br = null; 
            isr = new InputStreamReader(uc.getInputStream());
            br = new BufferedReader(isr);
            final int chk = 0; 
            String pat = "\"(.*)\": \"(.*)\",";
            Pattern pattern = Pattern.compile(pat);
            Matcher matcher = null;
            Map map = new HashMap();

            while ((url = br.readLine()) != null)
            {
                if ((chk >= 0) && ((chk < end))) {
                    matcher = pattern.matcher(url);
                    if(matcher.find()) {
                        map.put(matcher.group(1), matcher.group(2));
                    }
                    //r.append(url).append('\n');
                }
            }
              System.out.println(map.get("email")); 
              String email=(String) map.get("email"); 
              System.out.println(email);

5 Answers 5

5

Always use Generic type when using any collection or Map, unless of course you are using Java version older than 1.5. So, declare your Map as : -

Map<String, String> map = new HashMap<String, String>();

And then you won't need a typecast at all. map.get("email") will give you String type result only.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for suggesting the correct solution, not just fixing the code
3

Try this:

String email=map.get("email").toString();

Comments

1

Use toString() or "" with +,

String s = map.get("email").toString();

Or

String s = map.get("email")+"";

- And always prefer using Generics with Collection, so you enter specific type into the collection and get that specific type out of the collection.

Eg:

Map<String, String> map = new HashMap<String, String>();

2 Comments

If you want it to work with nulls, then use ""+, or String.valueOf(map.get("email")) (it will give the string "null" in both cases)
That's what I'm saying. Only map.get("email").toString() won't work with nulls
0

http://java.dzone.com/articles/two-ways-convert-java-map. Have a look at this link.Also Converting map values to string array convert Map Values into String Array.

Comments

0

It has been recommended to use type specific Map if you are working on java 1.5+. Your Map deceleration would be Map<String,String> map.

Map<String,String> map = new HashMap<>(); // Diamond operator available in java 7 
String email= map.get("email");


Map<String,String> map = new HashMap<String,String>(); // for java 5+ 
String email= map.get("email");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.