Читайте строки из текстового файла и сортируйте значения из строк по среднему (среднему), записывая их в новый текстовый файл - PullRequest
0 голосов
/ 29 июня 2018

У меня есть текстовый файл, который содержит следующий текст:

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

Ответы [ 4 ]

0 голосов
/ 29 июня 2018

Один вариант будет

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);
0 голосов
/ 29 июня 2018

Вы можете попробовать это. Сначала прочитайте каждую строку файла, а затем сопоставьте ее с классом 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
0 голосов
/ 29 июня 2018

Вы можете использовать этот подход:

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 + "]";
    }



}
0 голосов
/ 29 июня 2018

Вам нужно будет создать класс 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 
           }

   }

}

Сортируйте этот файл и распечатайте его в новый файл.

...