无法将数据存储到ArrayLists?

时间:2011-12-09 12:47:43

标签: java loops arraylist do-while

感谢所有的帮助人员,但现在问题的性质已经改变,使用Patrick的建议下面循环正在运行但它似乎没有将输入存储到相应的数组数据保持hetting替换为ArrayLists而不是去下一个位置进入ArrayList的任何建议?

  import java.util.ArrayList;
import java.util.Scanner;
public class Arrray {
    public static void main(String [] args){

        ArrayList<String> names;
        ArrayList<String> addr;


        do {
            names = new ArrayList<String>();
            addr = new ArrayList<String>();
            Scanner userInput = new Scanner(System.in);
            System.out.println("Name and Adreess are: " + names.size() + "**"
                    + addr.size());
            System.out.println("please Enter Your Name :");
            names.add(userInput.next());
            System.out.println("please enter your Address  :");
            addr.add(userInput.next());

            System.out.println("Do you want to add another entry? :(y/n)" );
            String ans =userInput.next();  // get the value from the user using scanner class
            if(ans.equals("n") || ans.equals("N"))
                break;

        } while (true);
        int n = names.size();
        int a = addr.size();
        for(int i =0; i<n && i<a; i++ )
            System.out.println("Name and address are as below:  "+ names.get(i)+"**"+ addr.get(i));




    }

}

5 个答案:

答案 0 :(得分:1)

while(true)break statement

结合使用
do {
    if(input.next() == 'n'){
        break;
    }
} while(true);

答案 1 :(得分:0)

尝试捕获此用户的输入

System.out.println("Do you want to add another entry? :(y/n)");

并在while中使用该信息。

答案 2 :(得分:0)

从用户获取值,如果用户输入n,则不执行任何其他操作

System.out.println("Do you want to add another entry? :(y/n)" );
String ans = .... // get the value from the user using scanner class
if(ans.equalsIgnoreCase("n"))
     break;

答案 3 :(得分:0)

你必须做这样的事情:

String choice = "";
do {
    .
    .
    .
    .
    System.out.println("Do you want to add another entry? :(y/n)" );
    choice = userInput.next();
} while (!(choice.equals("n") || choice.equals("N")));

该行

choice = userInput.next();

将读取用户输入,以及用于比较输入的Stringequals方法。循环将继续,直到选择为Nn

答案 4 :(得分:0)

import java.util.ArrayList; import java.util.Scanner;

public class Array {

public static void main(String[] args) {

    ArrayList<String> name = new ArrayList<String>();
    ArrayList<Integer> phone = new ArrayList<Integer>();
    Scanner scanner = new Scanner(System.in);
    String answer = "";
    do {
        System.out.println("Please enter your name: ");
        name.add(scanner.next());
        System.out.println("Please enter your number: ");
        phone.add(scanner.nextInt());
        System.out.println("Do you want to add a directory y/n?");
        answer = scanner.next();
    } while (answer.equals("y") || answer.equals("Y"));
    if (answer.equals("y") || answer.equals("Y")); //want it to go back to start another direcotry here
    else {
        System.out.println("Thanks for adding to the directory");
        for (int i = 0; i < name.size(); i++) {
            System.out.print(name.get(i) + "\t");
            System.out.print(phone.get(i));
            System.out.println("");
        }
    }
}

}