2016-06-07 90 views
0

我想問用戶,如果他們想添加另一個人數組列表,「列表」。我的想法是循環詢問,然後將這些輸入添加到數組「inf」中,然後將該數組放入數組列表中。現在,當我重複代碼時,它會刪除以前的輸入;我怎樣才能做到這一點,所以它在每次迭代後都添加到數組列表中?重複用戶輸入,而將其添加到數組列表

public class test { 

    public static void main(String[] args) throws FileNotFoundException { 

     Scanner scan = new Scanner(System.in); 

     String ask = "no"; 

     do { 
      System.out.println("Add? (yes/no)"); 
      ask = scan.nextLine(); 
      if (ask.equals("yes")){ 

       //gather user info 
       System.out.println("Last Name: "); 
       String LastN = scan.nextLine(); 


       System.out.println("First Name: "); 
       String FirstN = scan.nextLine(); 


       System.out.println("# of Children:"); 
       String Children = scan.nextLine(); 


       System.out.println("Address:"); 
       String Adr = scan.nextLine(); 

       System.out.println("Phone #:"); 
       String Pho = scan.nextLine(); 

       Date dategather = new Date(); 
       String Date = String.format("%tD", dategather); 

       //Input the data into an array 
       String[] inf = {LastN,FirstN,Children,Adr,Date,Pho}; 

       ArrayList<String> list = new ArrayList<String>(); 

       Collections.addAll(list,inf); 

       System.out.println(list);    

      } 

      } while (ask.equals("yes")); 

     } 
    } 
+1

您正在每個循環中創建一個新的數組列表! – venture

+0

您需要外部陣列 –

+0

@SeekAddo我在循環上方創建了數組列表,它看起來像是起作用。在每次迭代之後,是否有一種方法可以在新行上顯示? – jgmills

回答

1

你想要申報do-while結構之外的列表,如果你想打印每行的條目 - 然後你做到這一點就在do-while結構的末尾:

//here you declare the list OUTSIDE: 

ArrayList<String> list = new ArrayList<String>(); 

    do { 
      System.out.println("Add? (yes/no)"); 
      ask = scan.nextLine(); 
      if (ask.equals("yes")){ 

       //gather user info 
       System.out.println("Last Name: "); 
       String LastN = scan.nextLine(); 

       System.out.println("First Name: "); 
       String FirstN = scan.nextLine(); 

       System.out.println("# of Children:"); 
       String Children = scan.nextLine(); 

       System.out.println("Address:"); 
       String Adr = scan.nextLine(); 

       System.out.println("Phone #:"); 
       String Pho = scan.nextLine(); 

       Date dategather = new Date(); 
       String Date = String.format("%tD", dategather); 

       //Input the data into an array 
       String[] inf = {LastN,FirstN,Children,Adr,Date,Pho}; 

       Collections.addAll(list,inf); 
       //so here you want to display what the person just entered: 
       System.out.println("You Have Entered : \n Last Name: "+LastN+" First Name: "+FirstN+" Children : "+Children+" Address: "+Adr+" Date of Birth: "+Date+" Phone Number: "+Pho);    

      } 

      } while (ask.equals("yes")); 

我希望這幫助你。請嘗試一下,讓我知道。

+0

是的,這工作,謝謝! – jgmills

+0

太好了。我很高興這有助於。快樂的編碼。 – ishmaelMakitla

1

您可以創建一個具有每個人的所有細節並將其添加到ArrayList的Person類。類似的東西: (提示:你可以讓小童人數爲整數,而不是字符串)

public class test 
{ 
    public static void main(String[] args) throws FileNotFoundException { 
     ArrayList<Person> list = new ArrayList<Person>(); 

     do 
     { 
      System.out.println("Add? (yes/no)"); 
      ask = scan.nextLine(); 
      if (ask.equals("yes")){ 
        System.out.println("Last Name: "); 
        String lastName= scan.nextLine(); 


        System.out.println("First Name: "); 
        String firstName= scan.nextLine(); 


        System.out.println("# of Children:"); 
        String children = scan.nextLine(); 


        System.out.println("Address:"); 
        String address= scan.nextLine(); 

        System.out.println("Phone #:"); 
        String phone= scan.nextLine(); 

        Date dategather = new Date(); 
        String date = String.format("%tD", dategather); 
        list.add(new Person(lastName, firstName, children,address, phone,date)); 


      } 
     }while (ask.equals("yes")); 
    } 

} 

public class Person 
{ 
    String lastName, firstName, children,address, phone,date 
    public Person(String lastName,String firsName,String children,String address,String phone, String date) 
    { 
     this.lastName=lastName; 
     this.firsName=firsName; 
     this.children=children; 
     this.address=address; 
     this.phone=phone; 
     this.date=date; 
    } 
} 

在你的代碼中的每一個循環的重新初始化每次ArrayList的是正在重置。

+0

感謝您的幫助,但我認爲這略高於我現在的Java範圍。如果這是有道理的,我喜歡做我能理解的事情。 – jgmills

+0

@jgmills當然,如果你在頂部聲明arraylist(在do-while循環之外),你會沒事的。 –

相關問題