2013-03-09 206 views
-1

我想檢查一個對象是否存在於一個鏈表中,並根據它是否存在來執行一個動作,但是,無論我做什麼,java都將所有對象視爲不同。主代碼在下面提供,我很確定邏輯中的錯誤在這個代碼中。文章和客戶類是非常標準的。如果列表包含具有標題的文章,則標誌變量應該爲真,但始終爲false。任何幫助將非常感激。java對象比較

import java.util.*; 
import java.io.*; 

public class Proj1 { 
public static void main(String[] args) throws FileNotFoundException { 

    LinkedList<Article> Articles = new LinkedList<Article>(); 
    LinkedList<Customer> Customers = new LinkedList<Customer>(); 
    ListIterator<Customer> it = Customers.listIterator(); 
    int id = 0; 
    String command = ""; 

    if (args.length == 0 || args[0] == null) { 
     System.out.println("Please give a valid command file"); 
    } else { 
     try { 
      Scanner reader = new Scanner(new FileInputStream(args[0])); 

      while (reader.hasNext()) { 
       String arg = reader.nextLine(); 
       arg.split(" "); 
       String[] commands = arg.split("\\s+"); 

       if (isInt(commands[0])) { 
        id = Integer.parseInt(commands[0]); 
        command = commands[1]; 
        Customer temp = new Customer(id); 
        if (Customers.size() == 0) { 
         Customers.add(temp); 
        } else { 
         boolean flag = false; 
         for (int i = 0; i < Customers.size(); i++) { 
          if (id == Customers.get(i).getId()) { 
           flag = true; 
          } 
         } 
         if (flag == false) { 
          Customers.add(temp); 
         } 
        } 
       } else { 
        command = commands[0]; 
       } 
       // System.out.println(id+" "+command); 
       if (command.equalsIgnoreCase("borrow")) { 
        String title = ""; 
        int x = commands.length; 
        boolean flag = false; 
        for (int j = 2; j < x; j++) { 
         title += commands[j] + " "; 
        } 
        Article Article = new Article(title); 
        System.out.println(Articles.size()); 
        if (Articles.size() == 0) { 
         Articles.add(Article); 
        } else { 

         for (int i = 0; i < Articles.size(); i++) { 
          if (Article.getTitle() == Articles.get(i).getTitle()) { 
           flag = true; 
          } 
         } 
         if (flag == false) { 
          Articles.add(Article); 
         } 
        } 

        System.out.println(flag); 
        for (int i = 0; i < Customers.size(); i++) { 
         if (Customers.get(i).CustomerList().contains(title) && flag == true) { 
          Article.addToQ(Customers.get(i)); 

         } else { 
          Customers.get(i).CustomerBorrow(Article); 

         } 

        } 

        // System.out.println(title); 
       } else if (command.equalsIgnoreCase("return")) { 
        String title = ""; 
        int x = commands.length; 
        for (int j = 2; j < x; j++) { 
         title += commands[j] + " "; 
        } 
        Article Article = new Article(title); 
        if (Articles.size() == 0) { 
         Articles.add(Article); 
        } else { 
         boolean flag = false; 
         for (int i = 0; i < Articles.size(); i++) { 
          if (title == Articles.get(i).getTitle()) { 
           flag = true; 
          } 
         } 
         if (flag == false) { 
          Articles.add(Article); 
         } 
        } 
        for (int i = 0; i < Customers.size(); i++) { 
         if (id == Customers.get(i).getId()) { 
          Customers.get(i).CustomerReturn(Article); 
         } 
        } 
        // System.out.println(title); 
       } else if (command.equalsIgnoreCase("list")) { 
        for (int i = 0; i < Customers.size(); i++) { 
         if (id == Customers.get(i).getId()) { 
          System.out.println("Customer " + id 
            + " currently has: " 
            + Customers.get(i).CustomerList()); 
         } 
        } 
       } else if (command.equalsIgnoreCase("whohas")) { 
        String title = ""; 
        int x = commands.length; 
        for (int i = 1; i < x; i++) { 
         title += commands[i] + " "; 
        } 
        boolean flag = false; 
        int tempId = 0; 
        for (int i = 0; i < Customers.size(); i++) { 
         tempId = Customers.get(i).getId(); 
         if (Customers.get(i).CustomerList().contains(title)) { 
          flag = true; 
          tempId = Customers.get(i).getId(); 
         } 
        } 
        if (flag = true) { 
         System.out.println(tempId + " currently has " 
           + title); 
        } else { 
         System.out 
           .println("Currently no one has checked out " 
             + title); 
        } 

        // System.out.println(title); 
       } else if (command.equalsIgnoreCase("waitlist")) { 
        String title = ""; 
        int x = commands.length; 
        for (int i = 1; i < x; i++) { 
         title += commands[i] + " "; 
        } 
        for (int i = 0; i < Customers.size(); i++) { 
         if (Customers.get(i).CustomerList().contains(title)) { 
          Articles.get(i).printQ(); 

         } 
        } 
        // System.out.println(title); 
       } else if (command.equalsIgnoreCase("listCustomers")) { 
        System.out.println("Customers include: "); 
        for (int i = 0; i < Customers.size(); i++) { 
         System.out.println(Customers.get(i).getId()); 
        } 


       } else { 
        System.out.println("Command not recognized"); 
       } 

      } 

      reader.close(); 
     } 

     catch (Exception e) { 
      System.out.println("command not formatted correctly"); 
     } 
    } 

} 

public static boolean isInt(String string) { 
    try { 
     Integer.parseInt(string); 

    } catch (NumberFormatException nfe) { 
     return false; 
    } 
    return true; 
} 

} 

命令,如

29借用 「紐約時報」

29借用 「紐約時報」

允許重複,我試圖避免這種情況。謝謝。

+0

你需要清理大量的代碼,比如'Article.getTitle()== Articles.get(i).getTitle()'(假設'getTitle()'返回'String')。你應該使用'.equals()'。另外,'if(flag == false)'可以替換爲'if(!flag)'。 – 2013-03-09 01:59:37

回答

2

難道

if (Article.getTitle() == Articles.get(i).getTitle()) { 

打算比較字符串?這可以解釋爲什麼你的國旗總會回來。爲了比較在Java中,你應該使用equals(或equalsIgnoreCase爲不區分大小寫的比較)字符串

if (Article.getTitle().equals(Articles.get(i).getTitle()) { 

更多的背景信息here

+0

這是它,謝謝。我工作兼職做PHP,它讓我錯過這樣簡單的小事情更頻繁,非常感謝。 – kqualters 2013-03-09 02:07:48

1

我只是猜測,但我敢打賭,你要麼沒有覆蓋的CustomerArticle你沒有做正確equalshashCode

Joshua Bloch向您展示瞭如何在「有效的Java」的第3章。

我也想知道爲什麼你不選擇Set數據結構,如果重複不被允許。

+0

這是一個需要使用列表的任務,或者我會有 – kqualters 2013-03-09 02:07:08

+0

請注意我的第一條評論。 – duffymo 2013-03-09 03:58:47