2014-07-17 32 views
0

此代碼的目標是接受用戶輸入並將它們顯示爲單個記錄。我編寫了一個菜單,後面跟着2個方法,set和display,都使用Scanner類來填充arrayList,但不能使用display方法來打印它們。從不同功能打印ArrayList

這裏是代碼,請告訴我如何解決這個問題。

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

public class Records2 { 

    static ArrayList information = new ArrayList(); 

    public static void main(String[] args) { 
     int choice; 
     do { 
     System.out.println("1.Add \n 2.Delete \n 3.Update \n 4.Show \n Exit"); 

     //BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     //int choice; 
     System.out.println("Enter your Choice : "); 
     Scanner sc = new Scanner(System.in); 
     choice = sc.nextInt(); 

     switch (choice) { 

     case 1: System.out.println("Getting ready to Add a Record "); 
      set(); 
      break; 

     case 2: System.out.println("Getting ready to Delete a Record "); 
      //delete(); 
      break; 

     case 3: System.out.println("Getting ready to Update a Record "); 
      //update(); 
      break; 

     case 4: System.out.println("Here is your record "); 
      display(); 
      break; 

     case 5: System.out.println("Out we go."); 
      System.exit(0); 
      //exit(); 
      break; 

     default: System.out.println("Try again"); 
      break; 
     } 

     } while (choice > 5 || choice < 1); 

    } 

    public static void set() { 

     System.out.println("Please enter the ID, Name and Address of the person \n"); 

     Scanner readId = new Scanner(System.in); 
     int ID = readId.nextInt(); 

     Scanner readName = new Scanner(System.in); 
     String name = readName.nextLine(); 

     Scanner readAddress = new Scanner(System.in); 
     String address = readAddress.nextLine(); 

     //ArrayList information = new ArrayList(); 
     information.add(ID); 
     information.add(name); 
     information.add(address); 
    } 

    public static void display() { 

     System.out.println("here is your list" + information); 
     //how to differentiate from one set to another? 
    } 

} 
+0

你怎麼卡住了?你打算列出清單的哪個位置?爲什麼不能用for循環打印出信息ArrayList? –

+0

打印您的錯誤堆棧。 –

+0

預期顯示選項#4的選項是什麼?最後設定的結果? – sp00m

回答

2

它,因爲while循環!

while (choice > 5 || choice < 1); 

的代碼就會出來循環的選擇選擇= 1,這是一組()

所以顯示不會被調用後。

使用:while (choice > 0 && choice < 6)

+0

非常感謝!像魅力一樣工作。 – user1502

0

下面是完整的代碼 -

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

public class Records2 { 

    static ArrayList<Person> information = new ArrayList<Person>(); 

    public static void main(String[] args) { 
     int choice; 

     do { 
      System.out.println("\n-------------------------------\n Enter your Choice : "); 
      System.out.println("1.Add \n 2.Delete \n 3.Update \n 4.Show \n 5.Exit"); 
      Scanner sc = new Scanner(System.in); 
      choice = sc.nextInt(); 

      switch (choice) { 

      case 1: 
       System.out.println("Getting ready to Add a Record "); 
        set(); 
        break; 

      case 2: 
       System.out.println("Getting ready to Delete a Record. Enter id - "); 
       int choice2 = sc.nextInt(); 
       delete(choice2); 
       break; 

      case 3: 
       System.out.println("Getting ready to Update a Record "); 
       // update(); 
       break; 

      case 4: 
       System.out.println("Here is your record "); 
       display(); 
       break; 

      case 5: 
       System.out.println("Out we go."); 
       System.exit(0); 
       // exit(); 
       break; 

      default: 
       System.out.println("Try again"); 
       break; 
      } 

     } while (choice > 1 || choice < 5); 

    } 

    public static void set() { 

     System.out.println("Please enter the ID, Name and Address of the person \n"); 

     Scanner readId = new Scanner(System.in); 
     int ID = readId.nextInt(); 

     Scanner readName = new Scanner(System.in); 
     String name = readName.nextLine(); 

     Scanner readAddress = new Scanner(System.in); 
     String address = readAddress.nextLine(); 

     // ArrayList information = new ArrayList(); 

     Person person=new Person(); 
     person.setID(ID); 
     person.setName(name); 
     person.setAddress(address); 

     information.add(person); 



    } 

    public static void display() { 

     System.out.println("here is your list" + information); 
     // how to differentiate from one set to another? 
    } 

    public static void delete(int passedid) { 

//  System.out.println("here is your list" + information); 
     // how to differentiate from one set to another? 
     System.out.println("Before Deleting - "+information); 
     Iterator<Person> it=information.iterator(); 
     while (it.hasNext()) { 
      Person p = it.next(); 
      if(p.getID()==passedid){ 
       it.remove(); 
      } 
     } 

     System.out.println("AfterDeleting -"+information); 

    } 


} 

class Person{ 

    int ID; 
    String name; 
    String address; 

    public int getID() { 
     return ID; 
    } 
    public void setID(int iD) { 
     ID = iD; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getAddress() { 
     return address; 
    } 
    public void setAddress(String address) { 
     this.address = address; 
    } 
    @Override 
    public String toString() { 
     return "Person [ID=" + ID + ", name=" + name + ", address=" + address 
       + "]"; 
    } 



} 

這輸出 -

------------------------------- 
Enter your Choice : 
1.Add 
2.Delete 
3.Update 
4.Show 
5.Exit 
1 
Getting ready to Add a Record 
Please enter the ID, Name and Address of the person 

1 
A 
AA 

------------------------------- 
Enter your Choice : 
1.Add 
2.Delete 
3.Update 
4.Show 
5.Exit 
1 
Getting ready to Add a Record 
Please enter the ID, Name and Address of the person 

2 
B 
BB 

------------------------------- 
Enter your Choice : 
1.Add 
2.Delete 
3.Update 
4.Show 
5.Exit 
2 
Getting ready to Delete a Record. Enter id - 
1 
Before Deleting - [Person [ID=1, name=A, address=AA], Person [ID=2, name=B, address=BB]] 
After Deleing - [Person [ID=2, name=B, address=BB]] 
+0

這個信息變量是一個靜態的ArrayList變量,只有一個,所以不需要這個。 –

+0

但是如果用戶只想查看記錄呢? – user1502

+0

在這種情況下,您不應該存在該程序。當您在運行時將它存儲在ArrayList中。 –