Reputation: 105
my method findLargestDoubledChar takes in a string, and returns char c when c is the largest char that appears next to it's identical code. For example: findLargestDoubledChar(look)
returns o
and findLargestDoubledChar(aaxx)
returns x
. When there is no doubles, it's supposed to return '\u0000'
I have spent hours toying with this code and this is what I have. Where am I going wrong?
public static char findLargestDoubledChar(String string){
char largestDoubleChar = '\u0000';
String doubleChars = "";
char[] stringArray = string.toCharArray();
for(int x = 0; x < stringArray.length - 1; x++) {
for (int j = x + 1; j < stringArray.length; j++) {
if(stringArray[x] == stringArray[j]) {
System.out.println("Found Double!");
} else {
continue;
}
}
}
for (int i = 0; i < doubleChars.length(); i++) { //find largest of doubleChars
if (doubleChars.charAt(i) >= largestDoubleChar) {
largestDoubleChar = doubleChars.charAt(i);
} else {
largestDoubleChar = '\u0000';
continue;
}
}
return largestDoubleChar;
}
Upvotes: 0
Views: 74
Reputation:
Try this.
static final Pattern DOUBLE_PAT = Pattern.compile("(.)\\1");
public static char findLargestDoubledChar(String string){
return DOUBLE_PAT.matcher(string).results()
.map(x -> x.group().charAt(0))
.max(Comparator.naturalOrder()).orElse('\u0000');
}
Upvotes: 0
Reputation: 159086
It can be done in a single statement like this, which also supports Unicode characters from the supplemental planes, such as emojis.
public static String findLargestDoubledChar(String input) {
return Character.toString(Pattern.compile("(.)\\1").matcher(input)
.results().mapToInt(r -> r.group(1).codePointAt(0)).max().orElse('\0'));
}
Test
System.out.println(findLargestDoubledChar("look"));
System.out.println(findLargestDoubledChar("aaxx"));
System.out.println(findLargestDoubledChar("ššššššš"));
Output
o
x
š
Upvotes: 0
Reputation: 3
You have a few problems.
You aren't storing the doubled char when you find it. Example answer from modifying yours:
' public static char findLargestDoubledChar(String string){
char largestDoubleChar = '\u0000';
String doubleChars = "";
char[] stringArray = string.toCharArray();
for(int x = 0; x < stringArray.length - 1; x++) {
if (stringArray[x]==stringArray[x+1]){
System.out.println("Found Double");
doubleChars= doubleChars+ stringArray[x];
}
}
for (int i = 0; i < doubleChars.length(); i++) { //find largest of doubleChars
if (doubleChars.charAt(i) >= largestDoubleChar) {
largestDoubleChar = doubleChars.charAt(i);
} else {
largestDoubleChar = '\u0000';
continue;
}
}
return largestDoubleChar;
}'
Upvotes: 0
Reputation: 11
Looks like you're iterating over doubleChars in the second loop when it's being set to an empty string. Maybe something like this would work better:
public static char findLargestDoubledChar(String string){
char largestDoubleChar = '\u0000';
String doubleChars = "";
char[] stringArray = string.toCharArray();
for(int i = 0; i < stringArray.length - 1; i++) {
if (stringArray[i] == stringArray[i + 1]) {
System.out.println("Found Double!");
if (stringArray[i] > largestDoubleChar) largestDoubleChar = stringArray[i];
}
}
return largestDoubleChar;
}
Upvotes: 1