2017-03-10 182 views
1

我有一個包含3個屬性,優先級#,說明和參考#的對象/項目的ArrayList。該程序假設您可以根據項目的參考號打印Arraylist中的物品。出於某種原因,編譯器不會讓我遍歷ArrayList來查找匹配的項目。 我卡上(在方法內 'findbyrefer')的部分:按屬性搜索項ArrayList

for(Object list : list.getMyList()){ 
       if(list.getReference.equals(num)){ 
        System.out.println(list); 
       }  

我的主:

public class Main { 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    Scanner myscanner = new Scanner(System.in); 
    boolean exit = false; 
    boolean error = false; 
    boolean error1 = false; 

    String input = ""; 
    int num = 0; 
    System.out.println("Welcome to Your To-Do List!\n1 = Create New Item \n2 = Exit \n3 = Display Item \n4 = Delete Item"); 

    while(exit == false){ 
    Item item = new Item(); 
    do{ 
     try { 
      System.out.println("Enter a command "); 
      input = myscanner.nextLine(); 
      num = Integer.parseInt(input); 
      if(num == 1){ 
       item.reference(); 
       System.out.println("Reference: "+ item.getReference() + "\nDescription: " + item.getDescription() + "\nPriority: " + item.getPriority()); 
       error = true; 
      } 
      /** 
      * This creates the item 
      */ 
      else if(num == 2){ 
       exit = true; 
       System.out.println("Bye"); break; 
      } 
      else if(num == 3){ 
       item.findbyrefer(); 
       /** 
       * This is the part I'm stuck at 
       */ 
      } 
      else{ 
       error = true; 
       System.out.println("invalid"); 
      } 
     } 
     catch(Exception e){ 
      System.out.println("invalid input"); 
      error = true; 
     } 
    } 
    while(error); 
    } 
} 

我的物品類別:

public class Item { 
private Scanner myScanner; 
private int reference; 
private String description; 
private int priority; 
List list = new List(); 

public void setReference(int reference) { 
    this.reference = reference; 
} 
public int getReference() { 
    return reference; 
} 
public void setDescription(String description) { 
    this.description = description; 
} 
public String getDescription() { 
} 
public void setPriority(int priority) { 
    this.priority = priority; 
} 
public int getPriority() { 
    return priority; 
} 

public void reference(){ 
    boolean error = false; 
    int x = 0; 
    do{ 
     try{ 
      System.out.println("Assign this item with a reference number: "); 
      myScanner = new Scanner(System.in); 
      x=myScanner.nextInt(); 
      setReference(x); 
      error=false; 
      break; 
     } 
     catch(Exception e){ 
      error = true; 
      System.out.println("invalid"); 
     } 
    } while(error); 
    description(); 
} 

public void description(){ 
    boolean error = true; 
    while(error){ 
     try{ 
      System.out.println("Enter the description: "); 
      myScanner = new Scanner(System.in); 
      String input = myScanner.nextLine(); 
      setDescription(input); 
      error=false; 
      break; 
     } 
     catch(Exception e){ 
      error = true; 
      System.out.println("invalid"); 
      break; 
     } 
    } 
    priority(); 
} 

public void priority(){ 
    boolean error = false; 
    do{ 
     try{ 
      System.out.println("Assign this item with a priority number: "); 
      myScanner = new Scanner(System.in); 
      setPriority(myScanner.nextInt()); 
      error=false; 
     } 
     catch(Exception e){ 
      error = true; 
      System.out.println("invalid"); 
     } 
    } 
    while(error==true); 
    list.addItem(this); 
    System.out.println(list.getMyList()); 
} 
public void findbyrefer(){ 
    boolean error1 = false; 
    String input = ""; 
    int num = 0; 
    do{ 
     try{ 
      System.out.println("Enter the reference number of the item you want to show"); 
      myScanner = new Scanner(System.in); 
      input = myScanner.nextLine(); 
      num = Integer.parseInt(input); 
      for(Object list : list.getMyList()){ 
       if(list.equals(num)){ 
        System.out.println(list); 
       } 
       else{ 
        System.out.println("not working"); 
       } 
      } 
     } 
     catch(Exception e){ 
      error1 = true; 
      System.out.println("invalid"); 
     } 
    } 
    while(error1 = true); 
} 

} 我的列表類包含我的實際ArrayList:

public class List { 
public ArrayList<Object> MyList = new ArrayList<Object>(); 

public void setMyList(ArrayList<Object> myList) { 
    this.MyList = myList; 
} 
public ArrayList<Object> getMyList() { 
    return MyList; 
} 

public void addItem (Object t){ 
    getMyList().add(t); 
    } 

回答

0

爲列表的getReference方法添加()括號並使用(==)等於運算符。

for(Object list : list.getMyList()){ 
     if(list.getReference() == num)){ 
      System.out.println(list); 
     } 
} 
+0

這是*一*的問題。 –

0

Object沒有getReference方法。

因爲你的ArrayList包含Item S,讓它知道:

ArrayList<Item> myList = new ArrayList<>(); 
// ------^^^^^^ 

現在看看你的循環:

for(Object list : list.getMyList()){ 
    if(list.getReference.equals(num)){ 
     System.out.println(list); 
    } 
} 

我們需要:

  1. 變化ObjectItem
  2. 使用不同的標識比list的項目(只是爲了避免混淆)
  3. 呼叫getReference(添加()
  4. 使用==,不equals,檢查numequals爲對象)

所以:

for (Item item : list.getMyList()) { 
    if (item.getReference() == num){ 
     System.out.println(item); 
    } 
} 
+0

這是爲了讓你開始。必然存在其他後續問題,徹底的代碼審計對SO來說是無關緊要的。但是,如果遇到更多問題,請仔細閱讀錯誤消息,進行研究,檢查Java書籍和教程等。如果您確實遇到了問題,請通過[mcve]發佈有關您遇到的具體問題的新問題](注意「最小」)表明具體問題。 –