使用Comparable对Java文本文件进行排序

时间:2014-02-23 03:21:01

标签: java file sorting

我需要一个读取数据的程序,并使用基于提供的索引的quicksort按降序对文件进行排序,例如这是使用可比较的数据

adviser,32/60,125,256,6000,256,16,128,198,199
amdahl,470v/7,29,8000,32000,32,8,32,269,253
amdahl,470v/7a,29,8000,32000,32,8,32,220,253
amdahl,470v/7b,29,8000,32000,32,8,32,172,253
amdahl,470v/7c,29,8000,16000,32,8,16,132,132

我需要按照第5个索引(mmax)情况2和第6个(缓存)情况3和第9个索引(php)情况4按降序排序&打印第一个已经排序的案例1的索引 我的代码问题如下:

  • 它不基于索引进行排序
  • 它在运行时使用代码Arrays.sort(c);
  • 给出错误

请帮助提出建议 感谢

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Prog4 {
    static Scanner input;
    static File filename;
    /**
     * This function displays the menu for the user to choose an option from 
     */
    public void menu() {
        System.out.println("Option 1: Sort by VENDOR: ");
        System.out.println("Option 2: Sort decreasing number by MMAX: ");
        System.out.println("Option 3: Sort decreasing number by CACH: ");
        System.out.println("Option 4: Sort decreasing number by PRP: ");
        System.out.println("Option 5: Quit program");
    }

    /**
      * Constructor to handle the cases in the menu options
      * @throws FileNotFoundException 
      * @throws IOException 
      */
    public Prog4() throws FileNotFoundException {
        //Accepts user input
        Scanner in = new Scanner(System.in);

        //calls the menu method
        menu();

        //Initializes the run variable making the program loop until the user terminates the program
        Boolean run = true;

        //While loop
        while (run) {
            switch (in.nextInt()) {
            case 1:
                System.out.println("Option 1 selected");
                System.out.println("Sorted by vendor:");

                filename = new File("machine.txt");
                //Instantiate Scanner s with f variable within parameters
                //surround with try and catch to see whether the file was read or not
                try {
                    input = new Scanner(filename);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                //Instantiate a new Array of String type
                String array [] = new String[10];
                //while it has next ..
                while (input.hasNext()) {
                //Initialize variable 
                int i = 0;
                //store each word read in array and use variable to move across                                                               array                     array[i] = input.next();
                    //print 
                    System.out.println(array[i]);
                    //so we increment so we can store in the next array index
                    i++;
                }

            case 2:
                System.out.println("Press any key to continue");
                Scanner input2 = new Scanner(System.in);
                String x = input2.nextLine();
                if (x.equals(0)) continue;
                System.out.println("Option 2 selected") ;

                Computer[] c = new Computer[10];
                filename = new File("machine.txt");

                try {
                input = new Scanner(filename);
                } catch (FileNotFoundException e) {
                e.printStackTrace();
                }
                Arrays.sort(c);
                while (input.hasNextLine()) {
                    for (int i = 0; i < c.length; i++) {
                        System.out.println(c[i]);
                    }
                }
            }
        }
    }

    /**
      * Main method
      * @param args
      * @throws FileNotFoundException 
      */
    public static void main(String[] args) throws FileNotFoundException {
        //Calls the constructor
        new Prog4();
        //static Scanner input;
    }

    public static void quickSort(int arr[], int left, int right) {
        if (left < right) {
            int q = partition(arr, left, right);
            quickSort(arr, left, q);
            quickSort(arr, q+1, right);
        }
    }

    private static int partition(int arr[], int left, int right) { 
        int x = arr[left];
        int i = left - 1;
        int j = right + 1;
        while (true) {
            i++;
            while (i < right && arr[i] < x)
                i++;
            j--;
            while (j > left && arr[j] > x)
                j--;
            if (i < j)
                swap(arr, i, j);
            else
                return j;
            }
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

比较者类:

import java.util.Comparator;

class Computer implements Comparable<Computer> {

    private String vendor;
    private int mmax;
    private int cach;
    private int php;

    public Computer(int value) {
        this.mmax = value;
    }

    public String getVendor() {
        return vendor;
    }

    public void setVendor(String vendor) {
        this.vendor = vendor;
    }

    public int getMmax() {
        return mmax;
    }

    public void setMmax(int mmax) {
        this.mmax = mmax;
    }

    public int getCach() {
        return cach;
    }

    public void setCach(int cach) {
        this.cach = cach;
    }

    public int getPhp() {
        return php;
    }

    public void setPhp(int php){
        this.php = php;
    }

    @Override
    public int compareTo(Computer m) {
        if (mmax < m.mmax) {
            return -1;
        }

        if (mmax > m.mmax) {
            return 1;
        }

        // only sort by height if age is equal
        if (cach > m.cach) {
            return -1;
        }

        if (cach < m.cach) {
            return 1;
        }
        if (php > m.php) {
            return -1;
        }

        if (php < m.php) {
            return 1;
        }
        return 0;
    }

    public static Comparator<Computer> ComparemMax = new Comparator<Computer>() {
        @Override
        public int compare(Computer p1, Computer p2) {
            return p2.getMmax() - p1.getMmax();
        }
    };
}

2 个答案:

答案 0 :(得分:0)

最大的问题是计算机类没有为每一行读取实例化。

由于您希望根据用户输入使用不同的排序选项,因此您不能让Computer类确定compare方法,而是需要为每个排序选项创建单独的Comparator实现。接下来,使文件读取操作通用,并在每个选定案例的单独方法调用中将其抽象出来。我会把它变成List或Set,而不是计算机数组,因为你不想(想)知道前面的长度。

答案 1 :(得分:0)

我想详细列出步骤,以便您可以自己弄清楚每一步。你有很多正确的..但是有差距。

  1. 创建一个Computer类。它应该有一个构造函数,它接受一个String并使用分隔符','将它拆分,并在适用时将每个部分解析为String / int。 (最好是你解析并存储整个字符串..这意味着你的班级可以有10个字段)
  2. 创建一个空白ArrayList来存储计算机对象。
  3. 遍历文件并阅读readLine
  4. 使用表示while循环中文件中每一行的字符串
  5. 来调用计算机构造函数
  6. 将新计算机对象添加到计算机ArrayList
  7. 写出5种不同的比较器。
  8. 根据用户输入,实例化正确的比较器并将其传递给排序方法
  9. 打印已排序的数组
  10. 如果您仍然遇到问题,请提及您希望更清晰的特定点。