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);
user1726508
  • 83
  • 3
  • 9

5 Answers5

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

Try this:

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

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
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>();
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • 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) – Xavier Nov 09 '12 at 06:39
  • That's what I'm saying. Only `map.get("email").toString()` won't work with nulls – Xavier Nov 09 '12 at 06:45
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.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
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");
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103