Reputation: 13
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
Upvotes: 0
Views: 294
Reputation: 5616
As pointed out by @Ali there are multiple flaws in your code. Also there is no logic for unsorting the input.
Your unsorting will become easier with use of ArrayList instead of normal array. Here is a sample code which might serve your purpose.
import java.io.PrintWriter;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;
public class Assignment3 {
public static void main(String[] args) throws Exception{
Scanner stdIn = new Scanner(new File("sortedInput.txt"));
PrintWriter out = new PrintWriter("unsortedOutput.txt");
ArrayList<String> line = new ArrayList<String>();
System.out.println(stdIn.hasNextLine());
while (stdIn.hasNextLine()){
line.add(stdIn.nextLine());
}
//here is your unsorting
Collections.shuffle(line);
for(int i=0; i < line.size() ; i++)
out.println(line.get(i));
//Close printwriter once you are done writing everything
out.close();
}
}
Upvotes: 0
Reputation: 4411
while (stdIn.hasNextLine()) {
// for (int k = 0; k <= line.length - 1; k++) {
String str = stdIn.nextLine();
out.println(str);
// out.close(); --> here you are closing the out put writer so after first line it will be closed.
// }
}
Close scanner and printwriter outside the loop. otherwise after first line it will the writer will be closed and only first line will be printed.
Upvotes: 0
Reputation: 4817
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.
Upvotes: 0
Reputation: 69450
That line:
PrintWriter out = new PrintWriter("C:/Users/Vicki/Desktop/CountrySortedFormat.txt");
recreates the file. After this line your input file will be empty.
Upvotes: 1