2013-08-20 109 views
-1
import java.io.BufferedReader; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Scanner; 
import java.io.FileNotFoundException; 

import java.util.Collections; 
import java.util.Comparator; 
import java.util.Iterator; 

import java.util.List; 
import java.util.ArrayList; 
import java.util.StringTokenizer; 

public class database { 
    String fileName; 
    Scanner input; 
    String[][] data; 
    List<String> useful_list; 
    List<String> records; 
    ArrayList<Object> handles; 

    public database(String fileName) { 
     this.fileName = fileName; 
    } 

    public void openFile() { 
     try { 
      input = new Scanner(new File(fileName)); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      return; 
     } 
    } 

    public void readRecords() { 
     // Read all lines (records) from the file into an ArrayList 
     records = new ArrayList<String>(); 
     try { 
      while (input.hasNext()) 
       records.add(input.nextLine()); 

     } catch (Exception e) { 
      // TODO: handle exception 
     } 

    } 

    public void parseFields() { 
     String delimiter = ",\n"; 

     // Create two-dimensional array to hold data (see Deitel, p 313-315) 
     int rows = records.size(); // #rows for array = #lines in file 
     data = new String[rows][]; // create the rows for the array 
     int row = 0; 

     for (String record : records) { 
      StringTokenizer tokens = new StringTokenizer(record, delimiter); 
      int cols = tokens.countTokens(); 
      data[row] = new String[cols]; // create columns for current row 
      int col = 0; 
      while (tokens.hasMoreTokens()) { 
       data[row][col] = tokens.nextToken().trim(); 

       col++; 

      } 

      row++; 

     } 

    } 

    public static void main(String[] args) { 
     String filename = null; 
     String[] values = new String[4]; 
     String input = null; 

     BufferedReader reader = new BufferedReader(new InputStreamReader(
       System.in)); 

     try { 
      filename = reader.readLine(); 
      input = reader.readLine(); 
      values = input.split(","); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      System.out.println("Invalid Input"); 
      return; 
     } 

     int[] input1; 
     input1 = new int[4]; 

     try { 
      for (int j = 0; j < values.length; j++) { 
       input1[j] = Integer.parseInt(values[j]); 
      } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      System.out.println("Invalid Input"); 
      return; 
     } 

     if (input1[0] >= 4 || input1[0] <= 0) { 
      System.out.println("Invalid Input"); 
      return; 
     } 

     database file1 = new database(filename); 
     file1.openFile(); 
     file1.readRecords(); 
     file1.parseFields(); 
     file1.search(input1[1]); 
     if (file1.useful_list.size() == 0) { 
      System.out.println("Data Unavailable"); 
      return; 
     } 

     file1.sortarray(input1[0] - 1); 

     int width = input1[2]; 
     int skip = (input1[3] - 1) * width; 
     Iterator<Object> it = file1.handles.iterator(); 
     for (int i = 1; i <= skip; i++) { 
      if (it.hasNext()) { 
       it.next(); 
      } else { 
       System.out.println("Data Unavailable"); 
       return; 
      } 

     } 

     for (int j = 1; j <= width && it.hasNext(); j++) { 
      String[] a = (String[]) it.next(); 
      for (int i = 0; i < a.length; i++) 
       if(i<a.length-1) 
       System.out.print(a[i] + ","); 
       else 
        System.out.print(a[i]); 
      System.out.println(); 
     } 

    } 

    void sortarray(final int index) { 
     handles = new ArrayList<Object>(); 
     for (int i = 0; i < data.length; i++) 
      handles.add(data[i]); 

     Collections.sort(handles, new Comparator<Object>() { 
      public int compare(Object o1, Object o2) { 
       String[] a = (String[]) o1; 
       String[] b = (String[]) o2; 
       if (index == 1 || index == 0) { 
        int left = Integer.parseInt(a[index]); 
        int right = Integer.parseInt(b[index]); 
        return Integer.compare(left, right); //Line 165 
       } else { 

        if (a.length == 0 && b.length == 0) 
         return 0; 
        if (a.length == 0 && b.length != 0) 
         return 1; 
        if (a.length != 0 && b.length == 0) 
         return -1; 
        return a[index].compareTo(b[index]); 
       } 
      } 

      public boolean equals(Object o) { 
       return this == o; 
      } 

     }); 

    } 

    void search(int searchs) { 
     useful_list = new ArrayList<String>(); 

     for (int row = 0; row < data.length; row++) { 

      if (Integer.parseInt(data[row][0]) == searchs) { 
       // store in array list 
       useful_list.add(data[row][0] + "," + data[row][1] + "," 
         + data[row][2] + "," + data[row][3]); 

      } 
     } 
     if (useful_list.size() == 0) { 
      return; 
     } 
     String delimiter = ",\n"; 

     // Create two-dimensional array to hold data (see Deitel, p 313-315) 
     int rows = useful_list.size(); // #rows for array = #lines in file 
     data = new String[rows][]; // create the rows for the array 
     int row1 = 0; 

     for (String record : useful_list) { 
      StringTokenizer tokens = new StringTokenizer(record, delimiter); 
      int cols = tokens.countTokens(); 
      data[row1] = new String[cols]; // create columns for current row 
      int col1 = 0; 
      while (tokens.hasMoreTokens()) { 
       data[row1][col1] = tokens.nextToken().trim(); 

       col1++; 

      } 

      row1++; 

     } 

    } 

} 

此代码在eclipse上正常工作。
但如果我提交它为我的在线编译..
它显示编译时错误。编译在线编译时间错误?

错误信息
* database.java 163 cannotfindsymbolsymbol methodcompare INT INT位置类java.lang.Integer返回Integer.compare左右;^1error *

+0

你在哪里编译在线? –

回答

3

Integer.compare(int,int)是在Java 1.7中引入的。我希望你看到这个错误,因为Java 6或更早版本被用来编译代码。文档。他们自己(上面链接)展示了如何为早期的Java做这件事(你应该像这样在时间咨询他们)。

Integer.valueOf(x).compareTo(Integer.valueOf(y)) 
4

Integer.compare被引入到Java中版本1.7。很有可能在线编译器有编译器的早期版本

+0

如何解决plz的帮助...我时间不够了 –

+0

对于Java 1.7以前的版本,我们使用了'Integer.valueOf(left).compareTo(Integer.valueOf(right))' – Reimeus