import java.io.PrintStream; public class Test2B { public static void main(String[] args) { PrintStream out = System.out; final String WORD = args[0]; /* If you started with InversionCount, then you need to find a way to stop the innermost loop when you find two letters out of order. The standard way to do this is to use a boolean (in this case subAlpha). You also need to keep track of the longest alphabetic substring seen so far. */ // is the entire string in alphabetic order? boolean isAlphabetical = true; // the longest substring in alphabetic order int maxAlphabetical = 1; for (int i = 0; i < WORD.length() - 1; i++) { // is the substring starting at index i and ending at index j in // alphabetic order? boolean subAlpha = true; // length of the substring starting at index i int numAlphabetical = 1; for (int j = i + 1; j < WORD.length() && subAlpha; j++) { char ci = WORD.charAt(i); char cj = WORD.charAt(j); if (ci > cj) { subAlpha = false; isAlphabetical = false; } else { numAlphabetical++; } } if (numAlphabetical > maxAlphabetical) { maxAlphabetical = numAlphabetical; } } out.println(isAlphabetical); out.println("longest sequence of letters in alphabetical order: " + maxAlphabetical); } }