I thought using Object
was the only way to go, because it allows to store different types in it. This was desirable for the sorting of the object array since sorting int
's can't go wrong.
public class JavaApplication9 {
static private ReadFile readFile;
public static void main(String[] args) {
readFile = new ReadFile();
readFile.readnewFile();
readFile.wordRead();
readFile.convertToObject();
readFile.sortIng();
readFile.display();
}
}
second file:
public class WordsClass {
final public Object[][] finalTest;
private int nElems;
public WordsClass(String theDay, String theMonth, String theName, String theScore) {
finalTest = new Object[14][4];
nElems = 0;
}
public Object myObject(String a, String b, String c, String d) {
finalTest[nElems][0] = Integer.parseInt(a);
finalTest[nElems][1] = Integer.parseInt(b);
finalTest[nElems][2] = c;
finalTest[nElems][3] = Integer.parseInt(d);
nElems++;
return finalTest;
}
}
and third file:
public class ReadFile {
static private FileInputStream fstream;
static private DataInputStream in;
static private BufferedReader br;
static private String strLine;
static private Object[][] words;
static private int nElems;
WordsClass wordsClass = new WordsClass(null, null, null, null);
public ReadFile() {
words = new Object[14][4];
nElems = 0;
}
public void readnewFile() throws FileNotFoundException {
fstream = new FileInputStream("highscore.txt");
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));
// wordRead();
}
public Object wordRead() throws IOException {
try {
while ((strLine = br.readLine()) != null) {
words[nElems] = strLine.split(" ");
nElems++;
}
} catch (IOException e) {
}
br = null;
in = null;
fstream = null;
return words;
}
public Object convertToObject() {
for (Object[] word : words) {
wordsClass.myObject(word[0].toString(), word[1].toString(),
word[2].toString(), word[3].toString());
}
words = null;
return wordsClass;
}
public boolean sortIng() {
Arrays.sort(wordsClass.finalTest, new Comparator<Object[]>() {
@Override
public int compare(final Object[] w1, final Object[] w2) {
final Integer a = Integer.parseInt(w1[3].toString());
final Integer b = Integer.parseInt(w2[3].toString());
return a.compareTo(b);
}
});
return true;
}
public void display() {
for (Object[] finalTest : wordsClass.finalTest) {
for (Object finalTest1 : finalTest) {
System.out.print(finalTest1 + " ");
}
System.out.println("");
}
}
}
All this gives an output of:
- 24 1 James 1
- 24 1 James 2
- 24 1 James 3
- 24 1 James 3
- 24 1 James 3
- 24 1 James 7
- 24 1 James 11
- 24 1 James 41
- 24 1 James 44
- 24 1 James 431
- 24 1 James 444
- 24 1 James 865
- 24 1 James 876
- 24 1 James 1234
Since sorting by String
didn't work as wanted. It sorted ok, but it gave the wrong result.
It sorted at 1's, so 1, 11, 1234, 2 etc.
With thanks to this CompareTo/Sorting 2D array of Strings AND Integers topic. Which helped me figuring Object
sorting out.
edit:
I cleaned up a bit. It is good practice to keep main
as clean as possible and only use it for calls to methods in other classes. Added private and public to reference's, and set a few things to null
that weren't needed anymore after their usage. All this speeds up performance. (a lot).