2014-11-16 128 views
0

我想從2個文本文件中獲取字符串內容並與表執行聯合。Java關係代數聯盟

但是,我的代碼不斷輸出table @ hexidecimal對象的位置,而不是文件內的字符串內容。

這裏是我的代碼示例:

/** 
* ----------------------------------------------------------------------------------------------------- 
* Purpose : This class is used to create a row for a two dimensional data 
* 
* ----------------------------------------------------------------------------------------------------- 
*/ 
class Table { 

    String ID = ""; 
    String name = ""; 

    Table(String ID, String name) { 
     this.ID = ID; 
     this.name = name; 
    } 
} 

/** 
* ----------------------------------------------------------------------------------------------------- 
* Purpose : This class is used to show how to work with two dimensional data 
* ----------------------------------------------------------------------------------------------------- 
*/ 
public class RelationalAlgebra { 

    /** 
    * It reads a two columns table into a two dimensional array 
    * 
    * @return ArrayList<Table> 
    * @throws IOException 
    */ 
    ArrayList<Table> getTable(String fileName) throws IOException { 
     ArrayList<Table> T1 = new ArrayList<Table>();   // creates an array list 

     File inFile = new File(fileName);      // creates a file object 
     Scanner scanner = new Scanner(inFile);     // Scanner is a reader class 

     int repetition = 1;    // used to skip the 1st line from  input file 
     while (scanner.hasNext()) {      // reads until not data 
      if (repetition == 1) {      // if 1st line, skips 
       scanner.next(); 
       scanner.next(); 
       repetition = 2; 
      } else {         // else reads each column 
       String ID = scanner.next(); 
       String name = scanner.next(); 
       T1.add(new Table(ID, name)); 
      } 
     } 
     scanner.close();       // close input stream 
     return T1;         // returns the new table in the form of ArrayList 
    } 

    /** 
    * It prints the content of an ArrayList<Table> 
    * 
    * @param t 
    */ 
    void printTable(ArrayList<Table> t) { 
     for (int i = 0; i < t.size(); i++) { 
      System.out.println(t.get(i).ID + "\t" + t.get(i).name); 
     } 
    } 

    /** 
    * An entry point for program execution 
    * 
    * @param args 
    */ 
    public static void main(String[] args) throws IOException { 
     RelationalAlgebra rel = new RelationalAlgebra();  // creates an object of this class 
     ArrayList<Table> T1 = new ArrayList<Table>();   // creates an instance of ArrayList 
     ArrayList<Table> T2 = new ArrayList<Table>(); 

     T1 = rel.getTable("C:/Users/workspace/RelationalAlgebra/src/T1.txt"); 

     T2 = rel.getTable("C:/Users/workspace/RelationalAlgebra/src/T2.txt"); 

     rel.printTable(T1); 
     rel.printTable(T2);// prints the newly created table 


     //Performs Union without duplicates  
     Set<Table> tableUnion = new LinkedHashSet<Table>(); 
     tableUnion.addAll(T1); 
     tableUnion.addAll(T2); 
     tableUnion.toString(); 

     System.out.println("Union : " + tableUnion); 
    } 
} 

OUTPUT:

//T1 
1 A 
26 Z 
3 C 

//T2 
1 A 
2 B 
3 C 

//From Set<Table> tableUnion 
Union list: [[email protected], [email protected], [email protected], [email protected], [email protected], [email protected]] 
+0

「合併」變量聲明在哪裏? – izilotti

+0

如果您將'Table'對象放入您的'Set'中,並在打印時獲得'Table @ 213423',則需要在'Table'中實現'toString()'方法並定義該表的內容應該如何被轉換成一個字符串 – GameDroids

+0

對不起合併被刪除,我只是更新它。感謝您的輸入,Gamedroids,我對toString的想法是一樣的,但將它應用到我的最後一個System.out.println語句中。我應該把toString放在表類中? –

回答

0

所以,如果你把簡單的StringsSet,爪哇知道做什麼和如何打印設置。不太清楚應如何打印內容的類別需要實施toString()方法。在你的情況類似的東西:

class Table { 

    String ID = ""; 
    String name = ""; 

    Table(String ID, String name) { 
     this.ID = ID; 
     this.name = name; 
    } 

    @Override 
    public String toString(){ 
     return ID+" - "+ name;  // you can give the string any form you want and you can print anything you want. All that is required is that you return a String in that method. 
    } 
} 

在另一方面:即使ID是通常用於數據庫 - Java中的變量應該總是以一個小寫字母開始。只有類名以大寫字母開頭。

+0

非常感謝您的幫助,並增加了有關Java,GameDroids的知識。我會盡力回覆你。 –

+0

優秀!有效!謝謝!愛你! –

+0

:)太棒了!很高興它有幫助! – GameDroids