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..

You are done with the reading data from file. :) The next step is to actually store the read-in data to a variable in order to do the sort & print out.

Look at your FileLine class, you should have a constructor and class's variables. The constructor is to create an object instance of the class and the variables are for you to store necessary data to be used later (such as sort, print out, etc.).

// Below is an example of how to construct your FileLine class.
// The variable 'ArrayList' may be used if you want, or you can use
// other type of List.

import java.util.ArrayList;
public class FileLine {
  String filename;
  ArrayList<Integer> numberArray;

  // a sample constructor
  public Fileline(String str) {
    filename = str;
    numberArray = new ArrayList<Integer>();
  }

  // I am not sure about having this method throwing 'Exception'.
  // It would be more clear to me that what kind of exception it may throw...
  // Anyway, because you could take out the incoming argument,
  // you no longer need the 'throws Exception'. You should be able to
  // handle the Exception inside the method.
  public  void readList() {
    ...
    // read in each line and break down to each token
    // then push each token onto the ArrayList
    ...
  }

  // Sort your 'numberArray' here.
  // This method should rearrange your numberArray data.
  public void sort() {
  }

  // Print out your 'numberArray' to the console.
  public void printOut() {
  }
}

// When you create a constructor like that, your call will be changed.
// In your ReadFileLine class, instead of doing these 2 lines
  FileLine doLine = new FileLine();
  doLine.readList(fileName);

// change them to
  FileLine doLine = new FileLine(filename);
  doLine.readList();
  doLine.printOut();  // print out what you originally read in
  doLine.sort();      // sort it
  doLine.printOut();  // print out the result from sorted
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.