如何将用户输入添加到数组

时间:2016-02-12 22:22:18

标签: java

public static void main(String[] args) {
        // TODO Auto-generated method stub
        KeyboardReader reader = new KeyboardReader ();
        double money = 0; //Cash Variable
        int Choice = 0; //Menu Choice
        Scanner AccountIn = new Scanner(System.in);  //Scanner for AccountNumbers
        ArrayList<String> AccountNumber = new ArrayList<String>();
        String accountValue = "";


                int x = 1;    

do{     
                 try{   
                    System.out.println("Create Account Number");
                    accountValue = AccountIn.nextLine();
                    if(accountValue.length()>0 && accountValue.length()<=9){ 
                       accountValue.add(AccountNumber); //Add UserInput to arrayList

                                System.out.println("Final Amount:"  +money);

我正在尝试添加用户输入&#39; accountValue&#39;进入ArrayList&#39; AccountNumber&#39;但是当我选择查看它时它不显示。

1 个答案:

答案 0 :(得分:2)

尝试替换它:

    accountValue.add(AccountNumber);

使用:

   AccountNumber.add(accountValue);

AccountNumber是ArrayList,accountValue是要存储的值。