2014-07-17 33 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中。 –