We help IT Professionals succeed at work.

mapAB challenge options

Hi,


what is difference between below 3 lines




 map.put("ab", map.get("a").concat(map.get("b")));



map.put("ab", map.get("a")+ map.get("b"));

map.put("ab", "null" + map.get("b"));


I passed all tests
http://codingbat.com/prob/p107259
Looking for ways to improve my code and also avoid Null pointer exceptions


Expected	Run		
mapAB({"a": "Hi", "b": "There"}) → {"a": "Hi", "ab": "HiThere", "b": "There"}	{"a": "Hi", "ab": "HiThere", "b": "There"}	OK	
mapAB({"a": "Hi"}) → {"a": "Hi"}	{"a": "Hi"}	OK	
mapAB({"b": "There"}) → {"b": "There"}	{"b": "There"}	OK	
mapAB({"c": "meh"}) → {"c": "meh"}	{"c": "meh"}	OK	
mapAB({"a": "aaa", "ab": "nope", "b": "bbb", "c": "ccc"}) → {"a": "aaa", "ab": "aaabbb", "b": "bbb", "c": "ccc"}	{"a": "aaa", "ab": "aaabbb", "b": "bbb", "c": "ccc"}	OK	
mapAB({"ab": "nope", "b": "bbb", "c": "ccc"}) → {"ab": "nope", "b": "bbb", "c": "ccc"}	{"ab": "nope", "b": "bbb", "c": "ccc"}	OK

Open in new window


please advise
Comment
Watch Question

rrzstudent
CERTIFIED EXPERT

Commented:
I passed all tests
Good!
Looking for ways to improve my code
Please post your code so we can look at it.  
and also avoid Null pointer exceptions
You can avoid NullPointerExceptions by checking for null before using a variable.  For example:
import java.util.*;
public class T {
    public static void main(String arg[]){
		String myString = null;
		System.out.println(myString);
		if(myString != null)System.out.println(myString.toUpperCase());
		try{System.out.println(myString.toUpperCase());}
		catch(NullPointerException npe){
			System.out.println("NPE was caught");
		}
		myString = "hello!";
		System.out.println(myString.toUpperCase());
    }
}

Open in new window

the output is
null
NPE was caught
HELLO!

Open in new window

what is difference between below 3 lines
The first two lines do the same thing. If you were going to use them a million times, then we could talk about performance.  Maybe one is quicker than the other.
The last one
map.put("ab", "null" + map.get("b"));

Open in new window

is confusing to me. Why would you want to concatenate a String that contains the word null?  Do you see the difference between
String myString = null;

Open in new window

and
String myString = "null";

Open in new window

?
Concatenating null doesn't make sense either.  Maybe, you thinking of when null is returned from one of the get methods  
map.put("ab", map.get("a")+ map.get("b"));

Open in new window

  the key  ab could have a value something like "nullxxxxx"
rrzstudent
CERTIFIED EXPERT

Commented:
gudii9, said that he would close some of his open questions. See
https://www.experts-exchange.com/questions/29049217/mapAB2-errors.html     
I count over 30 open questions at this time.

Author

Commented:
closing
rrzstudent
CERTIFIED EXPERT

Commented:
You didn't post your code for this challenge.

Author

Commented:
oops here you go. i pass all tests
public Map<String, String> mapAB(Map<String, String> map) {
  if(map.containsKey("a")&&map.containsKey("b")){
    map.put("ab",map.get("a")+map.get("b"));
  }
  return map;
}

Open in new window

student
CERTIFIED EXPERT
Commented:
Your code looks good. I think most java guys would do it exactly like you did it. No chance of NullPointerException in that code(assuming that the map is not null).
Please keep closing your old questions. You will get better help.
rrzstudent
CERTIFIED EXPERT

Commented:
I counted 38 questions open at this time.
Why do you do this?

Author

Commented:
closing

Explore More ContentExplore courses, solutions, and other research materials related to this topic.