簡體   English   中英

從文本文件中讀取兩列並對java中的兩列進行排序

[英]read two columns from a text file and sort both columns in java

18 14
19 15
20 16
21 17
22 18
23 19
24 20
25 20
25 21

47 44
48 44
48 45
49 44

49 43
49 42
50 42
50 43
51 43
53 40
53 39
53 38
54 38
54 39

我的工作是我應該閱讀這個輸入文件,刪除空白行並在這里對我的java代碼進行排序我的java代碼試圖讀取文件並打印出它的值:這里我將整數存儲在不同的數組中,代碼是:

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

public class read
 {
        public static void main(String[] args) throws Exception 
        {
        System.out.println("Enter file name");
        DataInputStream dis=new DataInputStream(System.in);
        String dir1=dis.readLine();
        File infile = new File(dir1);
        System.out.println("Enter output file name");
        DataInputStream dis2=new DataInputStream(System.in);
        String dir3=dis.readLine();
        String path="E:/photos";
        String newpath=path + "/" +dir3;
        File outfile = new File(newpath);
        int newcount=0,newcount1=0;
        FileReader fr=new FileReader(infile);
        BufferedReader fr11= new BufferedReader(fr);
        FileWriter fw = new FileWriter(outfile);
        BufferedWriter bufferFileWriter  = new BufferedWriter(fw);
        Scanner input = new Scanner(infile);
        String[] outputArray1 = new String[31];
        String[] outputArray2 = new String[31];
        int i = 0;
               while (input.hasNextLine()) 
                {
                        String line = input.nextLine();
                        if(line.length() > 0)
                {
                        String[] columns = line.split(" ");
                        System.out.println("my first column : "+ columns[0] );
                        System.out.println("my second column : "+ columns[1] );
                        outputArray1[i] = columns[0];
                        outputArray2[i] = columns[1];
                        i++;
                }
                }
                String[][] temp = new String[2][];
                temp[0]= outputArray1;
                temp[1]= outputArray2;
               for (int k=0;k<2;k++)
        for (int j=0;j<i;j++)
        {
System.out.println("new row"+k+"new col"+j+"value="+temp[k][j]);
        }
        if (temp.length > 0) {
            for (int m = 0; m< temp[0].length; m++) {
                for (int n = 0; n< temp.length; n++) {
                    System.out.print(temp[n][m] + " ");
                }
                System.out.print("\n");
            }
        }
                       fr.close();
                       fw.close();
                    bufferFileWriter.close();
}
}

你可以采用這種方法,創建一個List<Item> ,其中Item是一個包含2個Column值的類型。 (x1和x2)

然后編寫一個compareTo(Item o) ,它比較在compare中呈現給它的兩個Item對象的x1值,如果給出了明確的答案,則返回該答案。

public class Item implements Comparable<Item> {
    private Integer int1;
    private Integer int2;

    @Override
    public int compareTo(Item o) {
        return int1 > (o.int1);
    }
}

希望這可以幫助。

以下內容可能會讓您了解自己可以做些什么。 此示例使用Java 7的一些功能。排序是C1 ASC, C2 DESC如SQL ORDER BY ,其中C1是第一列, C2是第二列。

public static class Row {
    public Row(String c1, String c2) {
        this.c1 = c1;
        this.c2 = c2;
    }
    String c1;
    String c2;
}

public static void main(String[] args) throws Exception {
    JFileChooser fileChooser = new JFileChooser();
    int option = fileChooser.showOpenDialog(null);
    if (option == JFileChooser.APPROVE_OPTION) {
        Path inputPath = fileChooser.getSelectedFile().toPath();
        option = fileChooser.showSaveDialog(null);
        if (option == JFileChooser.APPROVE_OPTION) {
            Path outputPath = fileChooser.getSelectedFile().toPath();
            sortAndSave(inputPath, outputPath);
        }
    }
}

public static void sortAndSave(Path inputPath, Path outputPath)
        throws IOException {
    List<String> lines = Files.readAllLines(inputPath,
            StandardCharsets.UTF_8);
    List<Row> rows = new ArrayList<>();
    for (String line : lines) {
        String[] array = line.split("\\s+");
        rows.add(new Row(array[0], array[1]));
    }
    Collections.sort(rows, new Comparator<Row>() {
        public int compare(Row r1, Row r2) {
            // C1 ASC, C2 DESC
            if (r1.c1.compareTo(r2.c1) == 0) {
                return r2.c2.compareTo(r1.c2);
            }
            return r1.c1.compareTo(r2.c1);
        }
    });
    try (BufferedWriter writer = Files.newBufferedWriter(outputPath,
            StandardCharsets.UTF_8);
            PrintWriter out = new PrintWriter(writer);) {
        for (Row row : rows) {
            out.println(row.c1 + " " + row.c2);
        }
        out.flush();
    }
}

如您所見,在java.util.Collectionsjava.util.Collections調用sort()方法時使用比較器。

輸出:

18 14
19 15
20 16
21 17
22 18
23 19
24 20
25 21
25 20
47 44
48 45
48 44
49 44
49 43
49 42
50 43
50 42
51 43
53 40
53 39
53 38
54 39
54 38

我的問題的答案是:

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

public class readnew
 {
        public static void main(String[] args) throws Exception 
        {
        System.out.println("Enter file name");
        DataInputStream dis=new DataInputStream(System.in);
        String dir1=dis.readLine();
        File infile = new File(dir1);
        System.out.println("Enter output file name");
        DataInputStream dis2=new DataInputStream(System.in);
        String dir3=dis.readLine();
        String path="E:/photos";
        String newpath=path + "/" +dir3;
        File outfile = new File(newpath);
        int newcount=0,newcount1=0;
        FileReader fr=new FileReader(infile);
        BufferedReader fr11= new BufferedReader(fr);
        FileWriter fw = new FileWriter(outfile);
        BufferedWriter bufferFileWriter  = new BufferedWriter(fw);
        Scanner input = new Scanner(infile);
        String[] outputArray1 = new String[31];
        String[] outputArray2 = new String[31];
        int i = 0;
               while (input.hasNextLine()) 
                {
                        String line = input.nextLine();
                        if(line.length() > 0)
                {
                        String[] columns = line.split(" ");
                        System.out.println("my first column : "+ columns[0] );
                        System.out.println("my second column : "+ columns[1] );
                        outputArray1[i] = columns[0];
                        outputArray2[i] = columns[1];
                        i++;
                }
                }
                String[][] temp = new String[2][];
                String[][] temp1 = new String[i][2];
                temp[0]= outputArray1;
                temp[1]= outputArray2;
               for (int k=0;k<2;k++)
        for (int j=0;j<i;j++)
        {
System.out.println("new row"+k+"new col"+j+"value="+temp[k][j]);
        }
        if (temp.length > 0) {
            for (int m = 0; m< temp[0].length; m++) {
                for (int n = 0; n< temp.length; n++) {
                temp1[m][n]=temp[n][m];
                    System.out.print(temp[n][m] + " ");
                }
                System.out.print("\n");
            }
        }
        System.out.println("transpose");
        for (int a=0;a<i;a++)
        for (int b=0;b<2;b++)
        {
        System.out.println("new row"+a+"new col"+b+"value="+temp1[a][b]);
        }
        System.out.println("sort");
    final Comparator<String[]> arrayComparator = new Comparator<String[]>() 
    {
        @Override
        public int compare(String[] o1, String[] o2)
        {
            return o1[1].compareTo(o2[1]);
        }
    };
    final Comparator<String[]> arrayComparator1 = new Comparator<String[]>() 
    {
        @Override
        public int compare(String[] o1, String[] o2) 
        {
            return o1[0].compareTo(o2[0]);
        }
    };
     Arrays.sort(temp1, arrayComparator);
     Arrays.sort(temp1, arrayComparator1);
     for(int p=0; p<i; p++)
        {
            for(int q=0; q<2; q++) 
            {
                System.out.print(temp1[p][q]+" ");
            }
            System.out.print("\n");
        }

                       fr.close();
                       fw.close();
                    bufferFileWriter.close();
}
}

輸出是:

18 14
18 14
19 15
19 15
20 16
20 16
21 17
21 17
22 18
22 18
22 18
22 18
23 19
23 19
23 19
23 19
24 20
24 20
24 20
25 20
25 20
25 20
25 21
25 21
25 21
26 21
26 21
26 21
27 21
27 21
27 21

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM