2014-02-23 38 views
0

文本文件,我需要一個程序,讀取數據和排序的基礎上提供例如該指數下降的使用快速排序順序文件是用可比Java的排序相媲美

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 我的代碼存在的問題如下:

  • 它不排序bas關閉索引
  • 它在運行時給我一個錯誤代碼: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(); 
     } 
    }; 
} 
+0

是否打印「空白」10次? – RaviH

+0

不,它打印一個陣列有10個索引,但幾行值 – custurd122000

回答

0

最大的問題是,計算機類不被實例化對於被讀取每一行。

由於您希望根據用戶輸入具有不同的排序選項,因此您不能讓Computer類確定比較方法,而是需要爲每個排序選項創建單獨的Comparator實現。接下來,使文件讀取操作通用,並在每個選定案例的獨立方法調用中將其抽象化。而不是一系列的計算機,我會把它做成一個List或一個Set,因爲你沒有(想要)知道前面的長度。

+0

列表 c = new ArrayList();我試圖做到這一點,然後用這行調用它,但它打印出nothingCollections。排序(C); \t \t而(input.hasNextLine()) \t \t { \t \t \t爲(字符串S:C) \t \t \t \t的System.out.println(一個或多個); \t} – custurd122000

+0

確切的說,因爲在你的代碼中沒有'new Computer()',並且沒有任何東西被添加到數組/列表中。 –

+0

我用這個代碼,但現在它打印出所有0,這是至少一個步驟列表 c = new ArrayList(); filename = new File(「machine.txt」); 嘗試{ \t \t \t input = new Scanner(filename); \t \t}趕上(FileNotFoundException異常E){ \t \t \t e.printStackTrace(); \t \t} 計算機e1 =新計算機(輸入); c.add(e1); \t \t而(input.hasNextLine()) \t \t { \t \t \t爲(計算機計算機:C) \t \t \t \t的System.out.println(computer.getMmax()); \t} – custurd122000

0

我想詳細說明這些步驟,以便您可以找出自己的每一步。你有很多它的權利..但有差距。

  1. 創建一個Computer類。它應該有一個構造函數,該構造函數接受一個String並使用分隔符「,」分割它,並根據需要將每個部分解析爲String/int。 (最好是解析和存儲整個字符串..這意味着你可以在你的班級中有10個字段)
  2. 創建一個空的ArrayList來存儲計算機對象。
  3. 迭代通過文件和readline
  4. 呼叫使用表示while循環
  5. 內文件中的每一行字符串的計算機構造函數添加新的計算機對象爲計算機ArrayList的
  6. 寫5個不同的比較。
  7. 根據用戶的輸入,實例化正確的比較,並把它傳遞給排序方法
  8. 打印數組排序

如果仍然面臨一個問題,何況在你喜歡更清晰的特定點。

相關問題