Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
Avatar of rrz
rrz
Flag of United States of America image

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"
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.
Avatar of gudii9

ASKER

closing
You didn't post your code for this challenge.
Avatar of gudii9

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I counted 38 questions open at this time.
Why do you do this?
Avatar of gudii9

ASKER

closing