So im working through this and im having troubles finishing it off. Essentially im reading a text file, and tokenizing the numbers in the file, but what needs to be done is for each line in the file to be store in its own Queue, and then the Queue's need to be compiled into one Queue, which then needs to be sorted from smallest to biggest number.
This is what I have so far:
Read File Line
import java.io.*;
public class ReadFileLine {
public static void main(String[] args) throws Exception {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in),1);
System.out.println("Enter name of the file to read from: ");
String fileName= keyboard.readLine();
System.out.println(fileName);//
FileLine doLine = new FileLine();
doLine.readList(fileName);
}
}
In String File
import java.io.*;
/**
* InStringFile makes file reading simpler. It allows
* information to be read one line at a time from a
* data file, as a String.
* @author CS1027
*/
public class InStringFile {
/**
* the handle to read in the file
*/
private BufferedReader in;
/**
* the next line of the file to be read
*/
private String nextLine;
/**
* Constructs the object that controls file reading
* Exits gracefullly if file not found or file cannot be read
* @param filename the name of the file to be read
*/
public InStringFile(String filename) {
try {
in = new BufferedReader(new FileReader(filename));
nextLine = in.readLine();
}
catch (FileNotFoundException ee){
System.out.println("File " + filename + " not found.");
System.exit(0);
}
catch (IOException e){
System.out.println("File " + filename + " cannot be read.");
System.exit(0);
}
}
/**
* Reads the next line of input as a String
* Exits gracefully if an error occurs while reading the file
* @return the next line from the input file
*/
public String read() {
String current = nextLine;
try {
nextLine = in.readLine();
}
catch (IOException e){
System.out.println("File cannot be read.");
System.exit(0);
}
return current;
}
/**
* Lookahead test for end of input
* @return true if end of file has been reached
*/
public boolean endOfFile() {
return (nextLine == null);
}
/**
* Closes the file (making it inaccessible though this InStringFile)
*/
public void close(){
try {
in.close();
in = null;
}
catch (IOException e){
System.out.println("Problem closing file.");
System.exit(0);
}
}
}
File Line
import java.util.StringTokenizer;
public class FileLine {
/**
* readFile method reads a string from a list from a file
* @param fileName filename of file that contains course information
*/
public void readList (String fileName) throws Exception {
// create object that controls file reading and opens the file
InStringFile reader = new InStringFile(fileName);
System.out.println("\nReading from file " + fileName + "\n");
// read data from file one line at a time
String line;
do
{
line = (reader.read());
System.out.println("The line is: " + line);
this.TokenizeString(line);
}while (!reader.endOfFile());
reader.close();
}
public void TokenizeString(String sentence){
String tk;
StringTokenizer tokens = new StringTokenizer(sentence);
System.out.println("The number of tokens per line is: " + tokens.countTokens());
while(tokens.hasMoreTokens()){
tk = tokens.nextToken();
// Do something with the token (number) here...
// hint: add the number to a queue...
System.out.println(tk);
}
}
}
Heres what appears in the text file:
56 12 134 45 22 78 48 42 11 9 56
30 3 1 10 33 38 7 55 51 29 19
40 89 35
283 170 189 77 18
144 13 66 93
This is what I get as a result:
The line is: 56 12 134 45 22 78 48 42 11 9 56
The number of tokens per line is: 11
56
12
134
45
22
78
48
42
11
9
56
The line is: 30 3 1 10 33 38 7 55 51 29 19
The number of tokens per line is: 11
30
3
1
10
33
38
7
55
51
29
19
The line is: 40 89 35
The number of tokens per line is: 3
40
89
35
The line is: 283 170 189 77 18
The number of tokens per line is: 5
283
170
189
77
18
The line is: 144 13 66 93
The number of tokens per line is: 4
144
13
66
93
And what I need is:
56 12 134 45 22 78 48 42 11 9 56 30 3 1 10 33 38 7 55 51 29 19 40 89 35 283 170 189 77 18 144 13 66 93
Followed by them in ascending order..