2017-03-10 130 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]发布有关您遇到的具体问题的新问题](注意“最小”)表明具体问题。 –