简体   繁体   中英

reading lines in from a text file and sorting them into a linked list java

I need to read in each line from a text file, and sort it according to length first, and then it's position in the original document, before adding the lines to a linked list.

The contents of the list must then be printed out, line by line, with a prefix indicating which line number is being printed out, and how many non-space characters are on the line.

Below is a sample I/O:

Input (i.e. contents of text file)

this
is
just
a
test

Output

1/1: a
2/2: is
3/4: this
4/4: just
5/4: test

You'll need to use a File and a Scanner. The code would look something like this:

import java.io.*;
import java.util.scanner;

public class ReadAndWrite {
    public static void main(String[] args) throws IOException {

        Scanner scan = new Scanner(new File("yourfile.txt"));

        int i = 1;
        while(scan.hasNext()) {
            String s = scan.nextLine();
            System.out.println("Line " + i + " says " + s + " and has " + s.length() + " characters.";
            i++;
        }
        System.out.println("/nNo more lines in the file.");
    }
}

Instead of solving it for you I will provide you with various links which will help you solve your assignment.

1) Readinga file in JAVA

2) Various string operations which can be performed on the string read : String operations

3) Sorting Collections in JAVA using compartors: Collection sorting

  1. I need to read in each line from a text file : Use FileReader and BufferedReader
  2. and sort it according to length first, and then it's position in the original document, before adding the lines to a linked list : create a HashMap with (String,lineNo) of original doc.
  3. Use Comparator to sort - first by length, then by line pos (get it from hashMap)using ternary operator .

  4. how many non-space characters are on the line : split the line using "s+" . add the lengths of all the sub arrays using a for loop.

  5. while printing from the arraylist, print count + nonSpaceChars in line + line .

Hope this will be sufficient

import java.util.*;
import java.io.*;

public class HelloWorld{

public static class mystruct {
    public String line;
    public int    number;
    public mystruct(String line, int count) {
        this.line = line;
        this.number = count;
    }
}

  public static void main(String []args){
     LinkedList<mystruct> list = new LinkedList<mystruct>();
     mystruct  temp;
     int count=0;
      try{
          FileInputStream fstream = new FileInputStream("input.txt");
          BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
          String readline;
          while ((readline = br.readLine()) != null) {
              count++;
              temp = new mystruct(readline, count);
            list.add(temp);
          }
          in.close();
         } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
         }      
         Collections.sort(list, new Comparator<mystruct>() {
             public int compare(mystruct o1, mystruct o2) {
                 if (o1.line.length() != o2.line.length())
                    return (o1.line.length() - o2.line.length());
                else {
                    return (o1.number - o2.number);
                }
             }
         });
          for (int i = 0; i < list.size(); i++) {
            temp = list.get(i);
            System.out.println(temp.line);
        }      
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM