如何创建联系人列表?

时间:2013-06-08 21:19:09

标签: java

到目前为止,这是我的代码:

Scanner scanner = new Scanner(System.in); //Scanning all user input

// Dialogue
System.out.println("Welcome to jAddress!");
System.out.println("Please tell me your name and phone number?");
String personalInfo = scanner.nextLine();
System.out.println("So your name & number is " + personalInfo);

//-----------------------------------------------------------------------------
String[] theList; // setting the type and name of the Array
theList = new String[6];// giving the array account of how many

/*Inputting the Name & Number Of Each Individual and storing it*/
personalInfo = theList[0]; // This one stores your personalInfo into theLis[0]

System.out.println("Do you Have a Name & Number You'd Like to Input? y/n");
String yesNo = scanner.nextLine();
String y = "yes"; String n = "no";
if(yesNo == y){
    System.out.println("Yay:D");
}else if(yesNo == n){
    System.out.println("GoodBye");
    //break;
}

System.out.println("Please Enter Name & Phone Number:");
theList[1] = scanner.nextLine(); //Storing Number & Name for theList[1]

System.out.println("Please Enter Name & Phone Number:");
theList[2] = scanner.nextLine(); //Storing Number & Name for theList[2]

System.out.println("Please Enter Name & Phone Number:");
theList[3] = scanner.nextLine(); //Storing Number & Name for theList[3]

System.out.println("Please Enter Name & Phone Number:");
theList[4] = scanner.nextLine(); //Storing Number & Name for theList[4]

System.out.println("Please Enter Name & Phone Number:");
theList[5] = scanner.nextLine(); //Storing Number & Name for theList[5]

System.out.println("If you want to access any name simply Press 0-5.");
int chooseContact = scanner.nextInt();

我设法将数组链接到用户输入。因此,用户可以手动输入名称和电话号码String,然后将其存储在其中一个阵列中。

但是,我希望最后的用户能够搜索阵列并打印出姓名和电话号码。例如,我希望用户输入1,然后输出数组theList[0]数字和名称。

我该怎么做?我正在寻找一种实现这一目标的简单方法,例如分配变量等。

1 个答案:

答案 0 :(得分:0)

你可以简单地使用用户输入的索引(+/- 1取决于0-5 / 1-6编号的情况)theList [index]