user1086245
user1086245

Reputation: 69

Converting a string with a mixture of upper and lower case characters java

Below is my code. Basically it worksout whether the characters are in ascending or descending order. It works perfectly if a lower case word is entered, but if for example aB is entered it is saying the letters are not in orde, when they clearly are!!! not really to sure and i'm starting to dispair at this!

    text.toLowerCase();

    while ( ! text.equals( "END" ) ) 

    {       

        String string = (text);
        char[] content = string.toCharArray();
        java.util.Arrays.sort(content);
        String sorted = new String(content);          


            if (text.equals(sorted))
            {
                System.out.print(text);
                System.out.print("     letters in ascending order");
                System.out.println();

            }

            else           
            {
                System.out.print((text));
                System.out.print("    letters not in ascending order");
                System.out.println();
            }

            System.out.println();
            System.out.print( "#Enter input text : " );
            text = BIO.getString();
        }
    }
}

Upvotes: 2

Views: 381

Answers (2)

stackh34p
stackh34p

Reputation: 8999

Why not use text.compareToIgnoreCase(sorted) == 0 ?

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160170

You need to save the value back to text (and check for a lower-case "end" since it was converted).

text = text.toLowerCase();
while (!text.equals("end")) { // ...

toLowerCase does not modify the original string, rather returns a lower-cased version.

Alternatively, if you want to preserve "END" to end:

lowered = text.toLowerCase();
while (!text.equals("END")) { 
    // ... etc ...
    text = BIO.getString();
    lowered = text.toLowerCase();
}

Upvotes: 3

Related Questions