Priyanshu Priyanshu - 20 days ago 6
Java Question

Taking all letters from a string converting into lowercase and then arranging them all in alphabetical order

My output gets the capital letters first and arranges those capital letters into alphabetical order than considers the lower case letters and arranges those into alphabetical order. I want to first convert the capital letters into lower case letters and then arrange them into alphabetical order as per in the desired output and changes are supposed to be made only in the collectLetters() method. Can anybody please help me on this? Thanks in advance.

public class QuizIID
{

public static String collectLetters(String text)
{
char[] charArray = text.toCharArray();
Arrays.sort(charArray);
String sortedString = new String(charArray);
return sortedString;
}


//runTest Method

private static boolean runTest(int testNum, String p0, boolean hasAnswer, String p1)
{
System.out.print("Test #" + testNum + ": [" + "\"" + p0 + "\"");
System.out.println("]");
String answer;
answer = collectLetters(p0);
boolean res;
res = true;

if (hasAnswer) {
System.out.println("Desired answer:");
System.out.println("\t" + "\"" + p1 + "\"");
}
System.out.println("Your answer:");
System.out.println("\t" + "\"" + answer + "\"");
if (hasAnswer) {
res = answer.equals(p1);
}
if (!res) {
System.out.println("DOESN'T MATCH!!!!");
} else if (hasAnswer) {
System.out.println("Correct!");
}
System.out.println();
return res;
}


//Main Method

public static void main(String[] args) {
int count = 0;

String p0;
String p1;

// ----- test 1 -----
p0 = "She sells sea shells";
p1 = "aeeeehhllllssssss";
if( runTest(1, p0, true, p1) ) count++;
// ------------------

// ----- test 2 -----
p0 = "ReMemBEr To IgnoRE CASe";
p1 = "abceeeeegimmnoorrrst";
if( runTest(2, p0, true, p1) ) count++;
// ------------------

// ----- test 3 -----
p0 = "12345";
p1 = "";
if( runTest(3, p0, true, p1) ) count++;
// ------------------

// ----- test 4 -----
p0 = "Programming III is fun";
p1 = "afggiiiiimmnnoprrsu";
if( runTest(4, p0, true, p1) ) count++;
// ------------------

// ----- test 5 -----
p0 = "Knights are better than Bishops";
p1 = "aabbeeeghhhiiknnoprrssstttt";
if( runTest(5, p0, true, p1) ) count++;
// ------------------

System.out.print( count + " out of 5" );
if (count == 5)
{
System.out.println("!");
}
else
{
System.out.println(".");
}
}
}


//Desired Output

----jGRASP exec: java QuizIID -Xlint:unchecked
Test #1: ["She sells sea shells"]
Desired answer:
"aeeeehhllllssssss"
Your answer:
"aeeeehhllllssssss"
Correct!

Test #2: ["ReMemBEr To IgnoRE CASe"]
Desired answer:
"abceeeeegimmnoorrrst"
Your answer:
"abceeeeegimmnoorrrst"
Correct!

Test #3: ["12345"]
Desired answer:
""
Your answer:
""
Correct!

Test #4: ["Programming III is fun"]
Desired answer:
"afggiiiiimmnnoprrsu"
Your answer:
"afggiiiiimmnnoprrsu"
Correct!

Test #5: ["Knights are better than Bishops"]
Desired answer:
"aabbeeeghhhiiknnoprrssstttt"
Your answer:
"aabbeeeghhhiiknnoprrssstttt"
Correct!

5 out of 5!

----jGRASP: operation complete.


//My Output

----jGRASP exec: java QuizIID
Test #1: ["She sells sea shells"]
Desired answer:
"aeeeehhllllssssss"
Your answer:
" Saeeeehhllllsssss"
DOESN'T MATCH!!!!

Test #2: ["ReMemBEr To IgnoRE CASe"]
Desired answer:
"abceeeeegimmnoorrrst"
Your answer:
"ABCEEIMRRSTeeegmnoor"
DOESN'T MATCH!!!!

Test #3: ["12345"]
Desired answer:
""
Your answer:
"12345"
DOESN'T MATCH!!!!

Test #4: ["Programming III is fun"]
Desired answer:
"afggiiiiimmnnoprrsu"
Your answer:
" IIIPafggiimmnnorrsu"
DOESN'T MATCH!!!!

Test #5: ["Knights are better than Bishops"]
Desired answer:
"aabbeeeghhhiiknnoprrssstttt"
Your answer:
" BKaabeeeghhhiinnoprrssstttt"
DOESN'T MATCH!!!!

0 out of 5.

----jGRASP: operation complete.

Answer

Replace the line

char[] charArray = text.toCharArray();

by

char[] charArray = text.replace( " ","" ).toLowerCase().toCharArray();

You could also use regex:

char[] charArray = text.toLowerCase().replaceAll( "[^\\p{Lower}]","" ).toCharArray();