简体   繁体   English

从文本文件中读取行并将它们排序到链表Java中

[英]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: 以下是一个示例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 1) 在JAVA中读取文件

2) Various string operations which can be performed on the string read : String operations 2)可以对读取的字符串执行各种字符串操作: 字符串操作

3) Sorting Collections in JAVA using compartors: Collection sorting 3)使用比较器在JAVA中对集合进行排序集合排序

  1. I need to read in each line from a text file : Use FileReader and BufferedReader 我需要从文本文件中读取每一行:使用FileReader和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. 并先根据长度对其进行排序,然后再将其在原始文档中的位置,然后再将这些行添加到链接列表中:使用原始文档的(String,lineNo)创建一个HashMap。
  3. Use Comparator to sort - first by length, then by line pos (get it from hashMap)using ternary operator . 使用Comparator首先按长度排序,然后使用三元运算符按pos行(从hashMap中获取)进行排序。

  4. how many non-space characters are on the line : split the line using "s+" . 该行上有多少个非空格字符:使用“ s +”分隔行。 add the lengths of all the sub arrays using a for loop. 使用for循环添加所有子数组的长度。

  5. while printing from the arraylist, print count + nonSpaceChars in line + line . 从arraylist打印时,在line + line中打印count + nonSpaceChars。

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);
        }      
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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