I need to sort the String array into least to greatest order by wordlength. I then need to print out the words next to their value.
Example Output:
1: I, U, K
6: Joseph, Delete
But I need help sorting the array first.
Code:
package Objects;
import java.io.*;
import java.util.*;
import java.util.Arrays;
import java.lang.Object;
public class WordLength {
public static void main(String[] args) throws Exception{
Scanner scan = new Scanner(new File("wordlength.dat"));
int thresh = scan.nextInt(); //Scans the first integer
String[] array = new String[thresh]; //Creates an empty array with length of the threshold
for(int i = 0; i < array.length; i++) {
array[i] = scan.nextLine();
/* I need to sort the array from least to
greatest here so I can use the if statement
below.
*/
//Prints the word array from least to greatest by length
if(array[i].length() == i) {
System.out.print(i + ": " + array[i]);
}
}
}
}