This project is supposed to have 3 separate main classes. It inputs a file of a list of countries that is sorted alphabetically, it outputs an unsorted file with lines rearranged randomly.
My first main class looks like this:
package assignment3;
import java.io.PrintWriter;
import java.io.File;
import java.util.Scanner;
public class Assignment3 {`
public static void main(String[] args) throws Exception{
Scanner stdIn = new Scanner(new File("C:/Users/Vicki/Desktop/CountrySortedFormat.txt"));
PrintWriter out = new PrintWriter("C:/Users/Vicki/Desktop/CountryUnsortedFormat.txt");
String[] line = new String[238];
while (stdIn.hasNextLine()){
for (int k = 0; k <= line.length-1; k++){
line[k]=stdIn.nextLine();
out.println(line[k]);
out.close();
}
}
}
}
My code doesn't have any visible problems but I tried printing out the array and got an array of "null". Am I doing something wrong?
EDIT: changed PrintWriter file name to CountryUnsortedFormat
Beside what @Jens says you have this problems too :
There multiple problem with your code. first of all, you have this line:
while (stdIn.hasNextLine())
so why there is this line?
for (int k = 0; k <= line.length-1; k++)
you have a loop already, and you dont need the second loop.
Also you close the output in every loop! What is that?
out.close();
you just need to close it in the end of the function!
your loop should be something like this :
int k = 0;
while (stdIn.hasNextLine()) {
// for (int k = 0; k <= line.length - 1; k++) {
line[k] = stdIn.nextLine();
out.println(line[k]);
k++;
// }
}
out.close();
Also after something like this your output is sorted, you didnt do any thing to make output unsorted.