我有一个文本文件,其中包含以下文本:
Input.txt:
name s1 s2 s3 s4
Jack 2 4 6 5
Alex 3 5 5 5
Brian 6 6 4 5
现在,最高的平均值是布莱恩:5.2;亚历克斯:4.5;杰克:4.25。 我的任务是获取每个人的平均人数,然后通过提高平均分数对人进行排序,然后使用排序后的值创建一个新的文本文件。
上面的示例在新的文本文件中必须看起来像这样。
Output.txt:
name s1 s2 s3 s4
Brian 6 6 4 5
Alex 3 5 5 5
Jack 2 4 6 5
到目前为止,我想出了2个解决方案,没有一个可以使任务完成。
第一个是:
public class Sort {
public static void main(String[] args) throws IOException {
int sortKeyIndex = 0;
Path inputFile = Paths.get("C:\\Users\\Desktop\\sample.txt");
Path outputFile = Paths.get("C:\\Users\\Desktop\\new-sample.txt");
String separator = " ";
Stream<CharSequence> sortedLines =
Files.lines(inputFile)
.skip(1)
.map(sorting -> sorting.split(separator))
.sorted(Comparator.comparing(sorting -> sorting[sortKeyIndex]))
.map(sorting -> String.join(separator, sorting));
Files.write(outputFile, sortedLines::iterator, StandardOpenOption.CREATE);
}
}
第二个是:
public class SortTestSecond {
private static BufferedReader theReader;
public static void main(String[] args) throws IOException {
try {
theReader = new BufferedReader(new FileReader("C:\\Users\\Desktop\\test2.txt"));
theReader.readLine();
String currLine = null;
while((currLine = theReader.readLine()) != null) {
System.out.println(currLine);
StringTokenizer strTok = new StringTokenizer(currLine, " ");
int theCount=strTok.countTokens();
int theArray[]=new int[theCount];
int i = 0;
while(strTok.hasMoreTokens() && i != theCount) {
theArray[i]=Integer.valueOf(strTok.nextToken());
i = i + 1;
}
int theSum = 0;
for(int j =0;j < theArray.length; j++) {
theSum = theSum + theArray[j];
}
float average = (float) theSum / theArray.length;
System.out.println("Average: " + average);
}
} catch(IOException err) {
err.printStackTrace();
} finally {
theReader.close();
}
}
}
答案 0 :(得分:2)
一个变种是
Path inputFile = Paths.get("C:\\Users\\Desktop\\sample.txt");
Path outputFile = inputFile.resolveSibling("new-sample.txt");
String separator = " ", newLine = System.getProperty("line.separator");
Pattern p = Pattern.compile(separator);
try(BufferedReader br = Files.newBufferedReader(inputFile);
BufferedWriter bw = Files.newBufferedWriter(outputFile, StandardOpenOption.CREATE)) {
bw.append(br.readLine()).append(newLine); // header
br.lines()
.sorted(Comparator.comparingDouble(line ->
-p.splitAsStream(line).skip(1).mapToInt(Integer::parseInt).average().orElse(-1)))
.forEachOrdered(s -> {
try { bw.append(s).append(newLine); }
catch(IOException ex) { throw new UncheckedIOException(ex); }
});
}
但是,排序操作仍然需要内存中的数据,即使看起来像是流畅的流式传输,也只是隐藏在这里。如果我们接受将全部数据存储在内存中这一事实,则可以将整个操作简化为:
Path inputFile = Paths.get("C:\\Users\\Desktop\\sample.txt");
Path outputFile = inputFile.resolveSibling("new-sample.txt");
String separator = " ";
Pattern p = Pattern.compile(separator);
List<String> lines = Files.readAllLines(inputFile);
lines.subList(1, lines.size())
.sort(Comparator.comparingDouble(line ->
-p.splitAsStream(line).skip(1).mapToInt(Integer::parseInt).average().orElse(-1)));
Files.write(outputFile, lines, StandardOpenOption.CREATE);
答案 1 :(得分:0)
您将必须创建一个名为Candidate的类,该类实现Comparable
class Candidate implements Comparable {
String name;
int [] values;
float average;
}
Candidate(String Name, int [] values) {
this.name = Name;
this.values = values;
this.average = getAverage();
}
public float getAverage() {
int sum = 0;
for(int c : values) {
sum += c;
}
return (float) sum/values.length;
}
@override
public int compareTo(Candidate c) {
if(c.average>this.average){
return 1;
} else {
return -1;
}
}
}
来到主类时,您需要为每行创建一个对象,并使用构造函数进行填充。
class main {
HashMap<String, Candidate> candidateList = new HashMap<String, Candidate>();
public static void main(String args[]) {
String FILENAME = "E:\\test\\filename.txt";
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
String [] currentLine = sCurrentLine.split(" ");
String name = currentLine[0];
int [] values = new int[currentLine.length-1];
for(int i=1; i<currentLine.length; i++) {
values[i-1] = Integer.valueOf(currentLine[i]);
}
Candidate c = new Candidate(name,values);
candidateList.put(name,c);// file converted to a list of candidates
}
}
}
排序该文件并将其打印到新文件。
答案 2 :(得分:0)
您可以尝试一下。首先读取文件的每一行,然后将其映射到Person
类。然后根据四个值的总和对每个Person
进行排序,因为它会分摊平均顺序。最后,将已排序的对象收集为String
。最后是将已排序的String
对象的Person
表示形式写入文件。同样值得为此使用Java 8。
try (Stream<String> stream = Files.lines(Paths.get("C:\\data\\sample.txt"))) {
final String sortedPeople = stream.skip(1).map(l -> l.split(" "))
.map(a -> new Person(a[0], Integer.parseInt(a[1]), Integer.parseInt(a[2]), Integer.parseInt(a[3]),
Integer.parseInt(a[4])))
.sorted(Comparator.comparingInt(Person::sum).reversed()).map(Person::toString)
.collect(Collectors.joining("\n"));
System.out.println(sortedPeople);
Path path = Paths.get("C:\\data\\new-sample.txt");
byte[] strToBytes = sortedPeople.getBytes();
Files.write(path, strToBytes);
} catch (IOException e) {
e.printStackTrace();
}
public class Person {
private final String name;
private final int valOne;
private final int valTwo;
private final int valThree;
private final int valFour;
public Person(String name, int valOne, int valTwo, int valThree, int valFour) {
super();
this.name = name;
this.valOne = valOne;
this.valTwo = valTwo;
this.valThree = valThree;
this.valFour = valFour;
}
public String getName() {
return name;
}
public int getValOne() {
return valOne;
}
public int getValTwo() {
return valTwo;
}
public int getValThree() {
return valThree;
}
public int getValFour() {
return valFour;
}
public int sum() {
return this.valOne + this.valTwo + this.valThree + this.valFour;
}
@Override
public String toString() {
return name + ", " + valOne + ", " + valTwo + ", " + valThree + ", " + valFour;
}
}
输出
Brian, 6, 6, 4, 5
Alex, 3, 5, 5, 5
Jack, 2, 4, 6, 5
答案 3 :(得分:0)
您可以使用这种方法:
package com.grsdev.stackoverflow.question180629.pack01;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ScoreTransformer {
private final static String INPUT_FILE="input.txt";
private final static String OUTPUT_FILE="output.txt";
private final static String SEPARATOR=" ";
public static void main(String[] args) throws IOException, URISyntaxException {
Path inputPath=Paths.get(INPUT_FILE));
Stream<String> sorted= Files
.lines(inputPath)
.skip(1)
.map(t->PersonConvertor.parseStringToPerson(t, SEPARATOR))
.sorted((p1,p2)->(int)(p2.getAverageScore()-p1.getAverageScore()))
.map(p->PersonConvertor.convertToString(p, SEPARATOR));
Files.write(Paths.get(OUTPUT_FILE), sorted.collect(Collectors.toList()));
}
}
class PersonConvertor {
public static Person parseStringToPerson(String line,String separator) {
Person blankPerson = new Person(null, 0, 0, 0, 0);
if(line==null) return blankPerson;
String[] array = line.split(separator);
if(array.length==5) {
return new Person (array[0],Integer.parseInt(array[1]),Integer.parseInt(array[2]),Integer.parseInt(array[3]),Integer.parseInt(array[4]));
}
return blankPerson;
}
public static String convertToString(Person p,String separator) {
if(p==null)return "";
String line=p.getName()+separator+p.getScore1()+separator+p.getScore2()+ separator+p.getScore3()+separator+p.getScore4();
return line;
}
}
class Person implements Comparable<Person>{
private String name;
private int score1,score2,score3,score4;
private float averageScore;
public Person(String name, int score1, int score2, int score3, int score4) {
super();
this.name = name;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
this.score4 = score4;
setAverageScore(((getScore1()+getScore2()+getScore3()+getScore4())/4));
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore1() {
return score1;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public int getScore2() {
return score2;
}
public void setScore2(int score2) {
this.score2 = score2;
}
public int getScore3() {
return score3;
}
public void setScore3(int score3) {
this.score3 = score3;
}
public int getScore4() {
return score4;
}
public void setScore4(int score4) {
this.score4 = score4;
}
public float getAverageScore() {
return averageScore;
}
public void setAverageScore(float averageScore) {
this.averageScore = averageScore;
}
public int compareTo(Person p) {
return (int) (this.averageScore-p.getAverageScore());
}
@Override
public String toString() {
return "Person name=" + name + ", score1=" + score1 + ", score2=" + score2 + ", score3=" + score3 + ", score4="
+ score4 + ", averageScore=" + averageScore + "]";
}
}