StringOrder.java

1 minute read

This program was created using the disgusting and outdated Java GUI named JOptionPane.

The program takes in a string and puts each and every letter into an array using the for loop. Then, it uses the Arrays library to create a copy of the array that is sorted and compare it to the array that was created using the for loop to see if it is sorted.

In the end, it returns a boolean.

The code has been modified slightly and now uploaded to the site.

<— Return Home

JOptionPane Code Snippet

/*
Author: ENTPRESTIGIOUS
Date Finished: April 13, 2021
Compiler: JDK 11.0.10 LTS
*/

import javax.swing.JOptionPane;
import java.util.Arrays;
public class StringOrder {
    public static void main(String[] args){
        boolean checker = true;
        JOptionPane.showMessageDialog(null, "Welcome!\nPlease press enter to continue...");
        while (checker == true){
        String word = JOptionPane.showInputDialog(null, "Please enter the phrase to check if it's in order!\nPress X on the top right to exit!");
        boolean result = isInOrder(word);
        JOptionPane.showMessageDialog(null, "Is the string in order?: " + result);
        }
    }
    public static boolean isInOrder(String str){
        boolean checker = false;
        int i = 0;
        String[] newArray = new String[str.length()];
        String[] newArray2 = new String[newArray.length];
        // grab separate letters from each phrase
        // check with the second letter to see if it's bigger
        for (i = 0; i < newArray.length; i++) {
            String separate = Character.toString(str.charAt(i));
            newArray[i] = separate;
            newArray2[i] = newArray[i];
        }
        Arrays.sort(newArray2);
        if (Arrays.equals(newArray2, newArray)){
            checker = true;
        }
        return checker;
    }
}

<— Return Home