私は3つのプロパティ、優先度#、説明、および参照#を持つオブジェクト/アイテムのArrayListを持っています。このプログラムでは、項目の参照番号に基づいてArraylistの項目を印刷することができます。何らかの理由で、コンパイラはArrayListを繰り返し処理して、一致する項目を見つけられません。 私は(メソッド内 'findbyrefer')にこだわっている部分:プロパティでアイテムのリストを検索する
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);
}
これは、問題の* 1つです。 –