I have a file that is a permuted word-list, formatted like this. The way it is formatted, when I open it up in a program like notepad, it appears to be not be spaced out at all, so for example, to the human eye, the first bit looks like this:
ATHROCYTESDISHLIKEIRRECOVERABLENESSESEMBRITTLEMENTSYOUNGSOVER
but when I copy and past it, it appears formatted like this:
ATHROCYTES
DISHLIKE
IRRECOVERABLENESSES
EMBRITTLEMENTS
YOUNGS
OVER
I am trying to load this file into an array so I can sort it. I am struggling as to how to break this up properly. I have found that using this code:
while (dis.available() != 0) {
System.out.println(dis.readLine());
}
prints out the document formatted correctly, just as if I would have copy and pasted it. I am using this code to try and load it in an array:
String[] store = sb.toString().split(",");
Since there are no commas, the words aren't separated correctly. Realizing this, I have also tried this code to try and split it at each new line:
String[] store = sb.toString().split(scan.nextLine());
Both of these give me the same result, the words being printed on the same line. Does anyone now how I could get my results properly formatted into an array?
I've included the rest of my code since it is possible that the problem originates elsewhere:
public class InsertionSort {
public static String[] InsertSort(String[] args) {
int i, j;
String key;
for (j = 1; j < args.length; j++) { //the condition has changed
key = args[j];
i = j - 1;
while (i >= 0) {
if (key.compareTo(args[i]) > 0) {//here too
break;
}
args[i + 1] = args[i];
i--;
}
args[i + 1] = key;
return args;
}
return args;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner scan = new Scanner(System.in);
System.out.println("Insertion Sort Test\n");
int n;
String name, line;
System.out.println("Enter name of file to sort: ");
name = scan.next();
BufferedReader reader = new BufferedReader(new FileReader(new File(name)));
//The StringBuffer will be used to create a string if your file has multiple lines
StringBuffer sb = new StringBuffer();
File file = new File(name);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
// this statement reads the line from the file and print it to
// the console.
System.out.println(dis.readLine());
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
while((line = reader.readLine())!= null){
sb.append(line);
}
//We now split the line on the "," to get a string array of the values
String[] store = sb.toString().split("/n");
System.out.println(Arrays.toString(store));
/* Call method sort */
InsertSort(store);
n = store.length;
FileWriter fw = new FileWriter("sorted.txt");
for (int i = 0; i < store.length; i++) {
fw.write(store[i] + "\n");
}
fw.close();
}
}