从其他函数打印ArrayList

时间:2014-07-17 11:24:04

标签: java arraylist java.util.scanner

此代码的目标是接收用户输入并将其显示为单个记录。我写了一个菜单,然后是2个方法,设置和显示,使用Scanner类填充arrayList但是无法使用display方法打印它们。

以下是代码,请告诉我如何解决此问题。

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?
   }

}

2 个答案:

答案 0 :(得分:2)

因为你的while循环!

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

选择choice = 1(用于set()

)后,代码将失去循环

所以永远不会调用显示器。

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

答案 1 :(得分: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]]