gudii9
asked on
firstchar fix
http://codingbat.com/prob/p168493
I am getting above error. What is int, int here. How to fix this error and improve my code. please advise
public Map<String, String> firstChar(String[] strings) {
Map<String, String> map=new HashMap<String, String>();
for(String str:strings){
int len=str.length();
map.put(""+str.substring(0,1), ""+str(0,len));
}
return map;
}
Compile problems:
cannot find symbol str(int,int) line:6
see Example Code to help with compile problems
I am getting above error. What is int, int here. How to fix this error and improve my code. please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Expected Run
firstChar(["salt", "tea", "soda", "toast"]) → {"s": "saltsoda", "t": "teatoast"} {"s": "soda", "t": "toast"} X
firstChar(["aa", "bb", "cc", "aAA", "cCC", "d"]) → {"a": "aaaAA", "b": "bb", "c": "cccCC", "d": "d"} {"a": "aAA", "b": "bb", "c": "cCC", "d": "d"} X
firstChar([]) → {} {} OK
firstChar(["apple", "bells", "salt", "aardvark", "bells", "sun", "zen", "bells"]) → {"a": "appleaardvark", "b": "bellsbellsbells", "s": "saltsun", "z": "zen"} {"a": "aardvark", "b": "bells", "s": "sun", "z": "zen"}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Guddi9. Are you not using an IDE?
Why not code in your IDE and then paste the solution into codingbat to test it?
Sort out your working practices before you venture into lambdas, garbage collection etc, as you usually do.
Why not code in your IDE and then paste the solution into codingbat to test it?
Sort out your working practices before you venture into lambdas, garbage collection etc, as you usually do.
ASKER
public Map<String, String> firstChar(String[] strings) {
Map<String, String> map=new HashMap<String, String>();
for(String str:strings){
String value=str;
int len=str.length();
if(map.containsKey(0)){
value=str.substring(0,1)+str.substring(0,len);
}
}
return map;
}
why above failing some tests
ASKER
below also fails
public Map<String, String> firstChar(String[] strings) {
Map<String, String> map=new HashMap<String, String>();
for(String str:strings){
String value=str;
String first=str.substring(0,1);
int len=str.length();
if(map.containsKey(first)) {
value=str.substring(0,1)+s tr.substri ng(0,len);
}
}
return map;
}
public Map<String, String> firstChar(String[] strings) {
Map<String, String> map=new HashMap<String, String>();
for(String str:strings){
String value=str;
String first=str.substring(0,1);
int len=str.length();
if(map.containsKey(first))
value=str.substring(0,1)+s
}
}
return map;
}
ASKER
what is diference with elow
for(String str:strings){
String firstChar = str.substring(0,1);
String value = str;
if (map.containsKey(firstChar )) {
value = map.get(firstChar)+str;
}
map.put(firstChar, value);
}
for(String str:strings){
String firstChar = str.substring(0,1);
String value = str;
if (map.containsKey(firstChar
value = map.get(firstChar)+str;
}
map.put(firstChar, value);
}
why above failing some tests
What does this mean?
if (map.containsKey(0)) {
what is diference with elow
Well for a start, your code nevers "puts" anything into the map, so it will always return an empty map.
And there are other logic issues...
ASKER
Well for a start, your code nevers "puts" anything into the map, so it will always return an empty map.i fixed as above
public Map<String, String> firstChar(String[] strings) {
Map<String, String> map=new HashMap<String, String>();
for(String str:strings){
String value=str;
String first=str.substring(0,1);
//int len=str.length();
if(map.containsKey(first)){
value=str.substring(0,1)+str.substring(0,len);
}
map.put(first,value);
}
return map;
}
And there are other logic issues...what other logical issues it has?
Map cannot have duplicate key right?
what is diference between below line
value = map.get(firstChar)+str;
and below line
value=str.substring(0,1)+s
please advise
ASKER
value = map.get(firstChar)+str;why are we adding get(firstChar) again adn again??
}
We are adding it "again and again" because otherwise we would lose the previous value in the map. As you said, a map CANNOT contain duplicate keys, and if you "put" a value into the map with the same key it overwrites whatever was in previously in there.
So, that's why if there is already a value in the map, we get that previous value (map.get(firstChar)) and then concatenate it with the current value ( + str), and then we put the entire concatenated value back into the map, replacing the previous value.
So, that's why if there is already a value in the map, we get that previous value (map.get(firstChar)) and then concatenate it with the current value ( + str), and then we put the entire concatenated value back into the map, replacing the previous value.
ASKER
for(String str:strings){as per me
String firstChar = str.substring(0,1);
String value = str;
if (map.containsKey(firstChar)) {
value = map.get(firstChar)+str;
}
map.put(firstChar, value);
}
We are adding it "again and again" because otherwise we would lose the previous value in the map. As you said, a map CANNOT contain duplicate keys, and if you "put" a value into the map with the same key it overwrites whatever was in previously in there.
So, that's why if there is already a value in the map, we get that previous value (map.get(firstChar)) and then concatenate it with the current value ( + str), and then we put the entire concatenated value back into the map, replacing the previous value.
firstChar(["aa", "bb", "cc", "aAA", "cCC", "d"]) → {"a": "aaaAA", "b": "bb", "c": "cccCC", "d": "d"}
1.aa
------
a a+aa===>a aaa
2.bb
--------------------
b b+bb===>b bbb
3. cc
-------------------------------
c c+cc============>c ccc
4.aAA
--------------------
firstchar a
if true ===>value aaa+aAA
a aaaaAA
a is key and aaaaAA should be value not aaaAA as per above logical flow
how to create mind map for this kind of map problems where my thinking is not going that deep?
please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
for(String str:strings){
String firstChar = str.substring(0,1);
String value = str;
if (map.containsKey(firstChar)) {
value = map.get(firstChar)+str;
}
map.put(firstChar, value);
}
for the first time first value of arrayif (map.containsKey(firstChar
ASKER
The first time for each firstChar, there won't be a previous entry and so that line DOESN'T get executed, the value just gets put in the map as is
i see it puts just str not firstchar+str as value as first time there is no match inside if condition
how you put this kind of challenge flow
is my above approach below ok and practical for complex problems involving multiple for loops and if , elsestatements etc?
firstChar(["aa", "bb", "cc", "aAA", "cCC", "d"]) → {"a": "aaaAA", "b": "bb", "c": "cccCC", "d": "d"}
1.aa
------
a (no concatenation performed, just put the value "aa" in the map)===>a aa
2.bb
--------------------
b (no concatenation performed, just put the value "bb" in the map)===>b bb
3. cc
--------------------------
c (no concatenation performed, just put the value "cc" in the map)============>c cc
4.aAA
--------------------
firstchar a
if true ===>value aa+aAA (this time we DO concatenate with the previous map value)
a aaaAA
if (map.containsKey(firstChar)) { is false right??
Yes, and since it is "false" we don't execute whats inside the if statement, we just go straight to line 7 (of what you just posted) and insert the "value" as is into the map.
Note, this is not JUST the first value of the array, but for each value of the array where we haven't previously seen that first character, so for the example...
firstChar(["aa", "bb", "cc", "aAA", "cCC", "d"])
^ ^ ^ ^
...for each marked value the condition will be "false" and the if statement WON'T be executed.
ASKER
makes sense
i see it puts just str not firstchar+str as value as first time there is no match inside if condition
how you put this kind of challenge flow
is my approach of design and understanding below OK and practical ? (for complex problems involving multiple for loops and if , else statements etc)
firstChar(["aa", "bb", "cc", "aAA", "cCC", "d"]) → {"a": "aaaAA", "b": "bb", "c": "cccCC", "d": "d"}
1.aa
------
a (no concatenation performed, just put the value "aa" in the map)===>a aa
2.bb
--------------------
b (no concatenation performed, just put the value "bb" in the map)===>b bb
3. cc
-------------------------- -----
c (no concatenation performed, just put the value "cc" in the map)============>c cc
4.aAA
--------------------
firstchar a
if true ===>value aa+aAA (this time we DO concatenate with the previous map value)
a aaaAA
The first time for each firstChar, there won't be a previous entry and so that line DOESN'T get executed, the value just gets put in the map as is
i see it puts just str not firstchar+str as value as first time there is no match inside if condition
how you put this kind of challenge flow
is my approach of design and understanding below OK and practical ? (for complex problems involving multiple for loops and if , else statements etc)
firstChar(["aa", "bb", "cc", "aAA", "cCC", "d"]) → {"a": "aaaAA", "b": "bb", "c": "cccCC", "d": "d"}
1.aa
------
a (no concatenation performed, just put the value "aa" in the map)===>a aa
2.bb
--------------------
b (no concatenation performed, just put the value "bb" in the map)===>b bb
3. cc
--------------------------
c (no concatenation performed, just put the value "cc" in the map)============>c cc
4.aAA
--------------------
firstchar a
if true ===>value aa+aAA (this time we DO concatenate with the previous map value)
a aaaAA
how you put this kind of challenge flow
is my above approach below ok and practical for complex problems involving multiple for loops and if , elsestatements etc?
Exactly how you write it down is up to you, but yes, the general approach is correct, i.e. you try and think exactly how the computer would, so in the above where you iterate through the array, you think each value in turn, and then for each value, try and work out what each line would do and where the execution would go next.
Once you get good at this, you may find that for fairly simple code like this, you don't need to write anything down at all, and you can just track the execution flow in your head.
ASKER
that makes sense
firstChar(["salt", "tea", "soda", "toast"]) → {"s": "saltsoda", "t": "teatoast"}
choosing above use case is more clear to my mind now than meaning less a aa b bb a aAA etc kind of values which wont register in mind. any thoughts?
all we are doing simply appending values one to seecond then third as long as we find same first char as key
firstChar(["salt", "tea", "soda", "toast"]) → {"s": "saltsoda", "t": "teatoast"}
choosing above use case is more clear to my mind now than meaning less a aa b bb a aAA etc kind of values which wont register in mind. any thoughts?
all we are doing simply appending values one to seecond then third as long as we find same first char as key
Yep, that's it
ASKER
Open in new window
above not appending later strings