So our professor has assigned us with a program which reads a text file he has provided us with; it sorts it, and creates a new file with the sorted stuff. He wants us to call three methods from the main i.e. read, sort, and write
I have done some of the work, but i'm confused what arguments to provide for io.sort. And how do i convert that text thing into an array to provide an argument Here's my code:
public void read() {
try {
Scanner myLocal = new Scanner(new File("dictionary.txt"));
while (myLocal.hasNextLine()) {
System.out.println(myLocal.nextLine());
}
} catch (IOException e) {
System.out.println(e);
}
}
public void sort(String[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
/* ERROR: When j == 0, j - 1 == -1, which is out of bounds */
if (arr[j - 1].compareTo(arr[j]) > 0) {
swap(j, arr);
}
}
}
}
public void swap(int j, String[] arr) {
String temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
public void write() {
try {
PrintStream writer = new PrintStream(new File("sorted.txt"));
for (int i = 0; i < 100; i++) {
writer.println(i);
}
writer.close();
} catch (IOException e) {
System.out.println(e);
}
}