簡體   English   中英

使用 GUI (java) 對文本文件的行進行排序

[英]Sorting lines of a text file using a GUI (java)

我有一個包含 20 名曲棍球運動員及其統計數據的文本文件。 在 GUI 中使用按鈕,我想在選定的統計數據中從最大到最小對玩家進行排序。 當我點擊一個按鈕時,我的文件沒有改變。

我在想我的變量 SortCol 有問題,但我不知道出了什么問題。

這是我的代碼示例:

@Override
    public void actionPerformed(ActionEvent e) { //method for what will happen when the Enter button is clicked
            Scanner console = new Scanner(System.in); //creates scanner named console
            Object[][] statArray = new Object[20][]; //created 2-Dimensional Object array named statArray so I can store different data types from my file
            for (int i = 0; i < 20; i++) { //created a loop that will run 20 times
                statArray[i] = new Object[8];
            }

            int lines = 0; //set variable named lines to hold a value of 0

            while (in.hasNextLine()) { //while loop that repeats until every line has been read
                String line = in.nextLine(); //creates a String Variable named line for each line
                Scanner readLine = new Scanner(line); //created Scanner named readLine

                while (readLine.hasNext()) { //created another while loop that repeats until a each token in a line has been read
                    statArray[lines][0] = readLine.next();//in these next 9 lines, I placed each token into my array, using my scanner
                    statArray[lines][1] = readLine.next();
                    statArray[lines][2] = readLine.nextDouble();
                    statArray[lines][3] = readLine.nextDouble();
                    statArray[lines][4] = readLine.nextDouble();
                    statArray[lines][5] = readLine.nextDouble();
                    statArray[lines][6] = readLine.nextDouble();
                    statArray[lines][7] = readLine.nextDouble();
                    lines++;
                }
                readLine.close(); //closes my readLine Scanner
            }

                if (e.getSource() == sortGames) {
                    sortCol = 2;

                } else if (e.getSource() == sortGoals) {
                    sortCol = 3;

                } else if (e.getSource() == sortAssists) {
                    sortCol = 4;

                } else if (e.getSource() == sortPoints) {
                    sortCol = 5;

                } else if (e.getSource() == sortShots) {
                    sortCol = 6;

                } else if (e.getSource() == sortShotPercentage) {
                    sortCol = 7;

                }
                final Comparator<Object[]> arrayComparator = new Comparator<Object[]>() { //compares a column in my array
                    @Override
                    public int compare(Object[] array1, Object[] array2) {
                        return (((Double) array2[sortCol]).compareTo((Double) array1[sortCol])); //returns the sorted array
                    }
                };

                Arrays.sort(statArray, arrayComparator);

                outputFile.printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s", "Player", "Name", "GP", "G", "A", "P", "S", "S%\n");//prints the header


                for (int rows = 0; rows < statArray.length; rows++) { //for loop used to print each row

                    outputFile.printf("\n%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s", statArray[rows][0], statArray[rows][1], statArray[rows][2], statArray[rows][3], statArray[rows][4], statArray[rows][5], statArray[rows][6], statArray[rows][7]);//prints the sorted stats in the file, nicely formatted
                    console.close(); //closes my console Scanner
                    in.close(); //closes my in Scanner
                }

首先,我建議創建一個 object 來代表您的玩家。 由於您使用的是 GUI,因此我建議您通過擴展AbstractTableModel然后使用它來構建 JTable 來創建自己的TableModel 。然后您可以按任何字段排序。 它應該看起來像這樣:

public class HockeyPlayer {
    private String name;
    private String team;
    private int games;
    private int goals;
    private int assists;
    private int points;
    private int shots;
    private float shotPercentage;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTeam() {
        return team;
    }
    public void setTeam(String team) {
        this.team = team;
    }
    public int getGames() {
        return games;
    }
    public void setGames(int games) {
        this.games = games;
    }
    public int getGoals() {
        return goals;
    }
    public void setGoals(int goals) {
        this.goals = goals;
    }
    public int getAssists() {
        return assists;
    }
    public void setAssists(int assists) {
        this.assists = assists;
    }
    public int getPoints() {
        return points;
    }
    public void setPoints(int points) {
        this.points = points;
    }
    public int getShots() {
        return shots;
    }
    public void setShots(int shots) {
        this.shots = shots;
    }
    public float getShotPercentage() {
        return shotPercentage;
    }
    public void setShotPercentage(float shotPercentage) {
        this.shotPercentage = shotPercentage;
    }
}

public class HockeyTableModel extends AbstractTableModel {

private List<HockeyPlayer> players = new ArrayList<>();

@Override
public int getColumnCount() {
    return 8;
}

@Override
public int getRowCount() {
    return players.size();
}

@Override
public Object getValueAt(int row, int col) {
    
    try {
        HockeyPlayer player = players.get(row);
        
        switch (col) {
        case 0:
            return player.getName();
        case 1:
            return player.getTeam();
        case 2:
            return player.getGames();
        case 3:
            return player.getGoals();
        case 4:
            return player.getAssists();
        case 5:
            return player.getPoints();
        case 6:
            return player.getShots();
        case 7:
            return player.getShotPercentage();
        }           
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public String getColumnName(int col) {
    switch (col) {
    case 0:
        return "Name";
    case 1:
        return "Team";
    case 2:
        return "Games";
    case 3:
        return "Goals";
    case 4:
        return "Assists";
    case 5:
        return "Points";
    case 6:
        return "Shots";
    case 7:
        return "Shot Percentage";
    default:
        return "";
    }
}
}

暫無
暫無

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

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