从外部文本文件中的Java排序 [英] Sorting in Java from external text file
问题描述
此数据被保存在data.txt中我试图写一个程序,可以安排
This data is saved in data.txt I am trying to write a program that can arrange
18b0885 // this is the registration number, bullet points to show indentation
SS844 Parallel Algorithms // These are course taken by student
SS555 Calculus for Distributed Computing
SS501 Quantum Communication
17b0585
SS828 Problem Based Programming
SS660 Genetic Computation
SS567 Hacking Quantum Network
17b2582
SS567 Hacking Quantum Network
SS876 Positronics
SS880 Quark-based Logic
这样的数据的大名单,而需要编写一个程序,总之这个数据升序由登记号和课程将遵循注册号。所以进出料口放就是这个样子。
A big list of data like this, And need to write a program to short this data is ascending order by registration number and course will follow the registration number. so the expected out put is like this.
17b2582
SS567 Hacking Quantum Network
SS876 Positronics
SS880 Quark-based Logic
17b0585
SS828 Problem Based Programming
SS660 Genetic Computation
SS567 Hacking Quantum Network
18b0885
SS844 Parallel Algorithms
SS555 Calculus for Distributed Computing
SS501 Quantum Communication
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Sort {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
Map<String, List<String>> map = new TreeMap<String, List<String>>();
String line = reader.readLine();//read header
while ((line = reader.readLine()) != null) {
String key = getField(line);
List<String> l = map.get(key);
if (l == null) {
l = new LinkedList<String>();
map.put(key, l);
}
l.add(line);
}
reader.close();
FileWriter writer = new FileWriter("sorted_numbers3.txt");
writer.write("");
for (List<String> list : map.values()) {
for (String val : list) {
writer.write(val);
writer.write("\n");
}
}
writer.close();
}
private static String getField(String line) {
return line.split(",")[0];//
}
}
以上程序了付诸这样的另一个文本文件
The above program out puts into another text file like this
SS501 Quantum Communication
SS555 Calculus for Distributed Computing
SS567 Hacking Quantum Network
SS567 Hacking Quantum Network
SS660 Genetic Computation
SS828 Problem Based Programming
SS844 Parallel Algorithms
SS876 Positronics
SS880 Quark-based Logic
17b2582
17b0585
18b0885
据短裤所有coureses由升序然后登记号码,但不是我想要的。我应该怎么改?
It shorts all the coureses by ascending order then registration numbers but not what I want. What should I change?
推荐答案
嗯,正如其他人说你的示例输出是不以任何排序顺序,我可以看到。如果你真的想连续的字母顺序,那么这将做到这一点。
Well, as others have said your example output is not in any sorted order I can see. If you actually want alphabetical order of serials, then this will do it.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Sorter {
static List<Data> read() throws FileNotFoundException, IOException {
BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
List<Data> list = new ArrayList<>();
String line;
Data data = null;
while ((line = reader.readLine()) != null) {
if (line.matches("\\s*")) {
continue; // skip blank lines
}
// Assume line that begins with space is a course.
if (Character.isSpaceChar(line.charAt(0))) {
// Add new course to data if it's there.
if (data == null) {
System.err.println("Missing serial for course " + line);
} else {
data.courses.add(line);
}
} else {
// Add completed data to list if there is one.
if (data != null) {
list.add(data);
}
// Make new data with this serial.
data = new Data(line);
}
}
// Add the last data to the list if there is one.
if (data != null) {
list.add(data);
}
return list;
}
public static void main(String[] args) {
try {
// Read.
List<Data> list = read();
// Sort based on serials.
Collections.sort(list, new Comparator<Data>() {
@Override
public int compare(Data a, Data b) {
return a.serial.compareTo(b.serial);
}
});
// Print.
for (Data data : list) {
data.print();
}
} catch (Exception ex) {
System.err.println("Read failed.");
}
}
}
// Local class to hold data: a serial and related courses.
class Data {
String serial;
List<String> courses;
Data(String serial) {
this.serial = serial;
courses = new ArrayList<>();
}
void print() {
System.out.println(serial);
for (String course : courses) {
System.out.println(course);
}
}
}
这篇关于从外部文本文件中的Java排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!