In my Java program, I have the following code:
String[] states = readFile("States.txt");
System.out.println(String.join(" ", states));
System.out.println(states.length);
Arrays.sort(states);
System.out.println(String.join(" ", states));
System.out.println(states.length);
Strangely enough, calling Arrays.sort()
from java.util.Arrays
causes many items to be removed from the list. When I run the code above, this is the output:
FL GA SC NC VA MD NY NJ DE PA CT RI MA VT NH ME AL TN KY WV OH MI MS AR MO KS NE IN IL WI MN LA TX OK IA SD ND NM CO WY ID AZ UT NV MT CA OR WA AL HI
50
AL AL AR AZ CA CO CT DE FL GA HI
50
I am very, very confused as to what's going on here. Why are only 11 items printed out? Is Arrays.sort()
removing items? Why would Arrays.sort()
do this? Why is the size of the array still 50
? Are the items being blanked out or something?
I assume that my readFile()
method works fine as the unsorted array prints out fine...
public static String[] readFile(String FileName) {
char[] cbuf = new char[200];
String[] array;
FileReader fr;
try {
fr = new FileReader(FileName);
try {
fr.read(cbuf);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String all = new String(cbuf);
array = all.split("\n");
return array;
}
The file I am reading from: https://nofile.io/f/8TO3pdnmS3W/States.txt MD5 starts with 8b961b5
The newline character at the end of the file, specifically after the last entry in the file "HI
", seems to be causing the problem. It can be solved in the readFile
function by using:
array = all.trim().split("\n");
Confirmed the 'artifact' behavior via the Online Java Compiler :
import java.util.Arrays;
public class MyClass {
public static void main(String args[]) {
// instead of using readFile() the array is defined here.
// note the \n on the last element
String[] states = {"FL", "GA", "SC", "NC", "VA", "MD", "NY", "NJ", "DE", "PA", "CT", "RI", "MA", "VT", "NH", "ME", "AL", "TN", "KY", "WV", "OH", "MI", "MS", "AR", "MO", "KS", "NE", "IN", "IL", "WI", "MN", "LA", "TX", "OK", "IA", "SD", "ND", "NM", "CO", "WY", "ID", "AZ", "UT", "NV", "MT", "CA", "OR",
"WA", "AL", "HI\n"};
System.out.println(String.join(" ", states));
System.out.println(states.length);
Arrays.sort(states);
System.out.println(String.join(" ", states));
System.out.println(states.length);
}
}
And the Output:
FL GA SC NC VA MD NY NJ DE PA CT RI MA VT NH ME AL TN KY WV OH MI MS AR MO KS NE IN IL WI MN LA TX OK IA SD ND NM CO WY ID AZ UT NV MT CA OR WA AL HI
50
AL AL AR AZ CA CO CT DE FL GA HI
IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY
50
Apparently the log used by @Arjun Kay had truncated the elements printed after the sorted element with the break-line character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With