繁体   English   中英

Java可比文本文件

[英]Java sorting text file with Comparable

我需要一个程序来读取数据并根据提供的索引使用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

而且我需要按降序对第五个索引(mmax)情况2和第六个(缓存)情况3和第九个索引(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();
        }
    };
}

最大的问题是,不会为读取的每一行实例化Computer类。

由于要根据用户输入具有不同的排序选项,因此不能让Computer类确定比较方法,而是需要为每个排序选项创建一个单独的Comparator实现。 接下来,使文件读取操作具有通用性,并在每个选定案例的单独方法调用中将其抽象化。 而不是计算机阵列,我将其设为列表或集合,因为您不(想要)知道前面的长度。

我想详细列出这些步骤,以便您自己找出每个步骤。 您做对了很多..但是有差距。

  1. 创建一个计算机类。 它应该具有一个构造函数,该构造函数采用单个String并使用分隔符','对其进行拆分,并在适用时将每个部分解析为String / int。 (最好解析并存储整个字符串。这意味着您的类中可以有10个字段)
  2. 创建一个空白ArrayList来存储Computer对象。
  3. 遍历文件并读取行
  4. 使用代表while循环中文件中每一行的String调用Computer构造函数
  5. 将新的Computer对象添加到计算机ArrayList
  6. 编写5个不同的比较器。
  7. 根据用户输入,实例化正确的比较器并将其传递给sort方法
  8. 打印排序的数组

如果仍然遇到问题,请提及您希望更清晰的特定点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM