Need help with String Trimming.. and displaying Keys on maps.
orking on learning recursion - and i have finally solved the issue.
The last issue I need to figure out are two minor issues that I can't seem to find out the problems on. After 2 hours of testing and no success... i'm over here.. seeking help and assistance on some pointers.
I have a method testR that accepts a string = symbol.
The method will split the String, if it has a '|', or white space.
But if, for example, the string symbol of this is passed int:
" T | W J Y P "
The method below fails to eliminate the trailing and starting white spaces between the letters. Wondering where I should be running it.
Code :
public String testR(String symbol){
String s = storeString.get(symbol);
String[] parts = s.split("[|]");
Random rand = new Random();
int pick = rand.nextInt(parts.length);
String x = parts[pick];
String[] space = x.split("[ \t]");
String rString = "";
for( int i=0; i < space.length; i++){
if(!grammarContains(space[i])){
rString = rString.concat(space[i].trim()+" ");
}else{
rString = rString.concat(testR(space[i].trim()));
}
}
return rString;
}
Then I have a method showKeys. Assume that I already have a map and keys/values stored.
I'm just simply trying to access the keys and display it out.
But i'm having no luck in that either.
Code :
public String showKeys(){
String s = "";
for (String nonTerminal : storeString.keySet() ) {
s.concat(nonTerminal+", ");
}
// s = "["+s+"]";
return s;
}
Thanks.
Re: Need help with String Trimming.. and displaying Keys on maps.
For the first problem (if I understand the problem) you may want to call trim after your first split on '|' eg String x = parts[pick].trim();
For the second problem you might have to elaborate to exactly what the problem is, but just looking at the code, you might want to use StringBuilder or StringBuffer as opposed to concat.