使用扫描程序接受字符串输入并存储在字符串数组中

时间:2013-01-11 14:10:34

标签: java arrays string java.util.scanner

是的,请有人帮帮我。我做了很多搜索,但无法在任何地方找到解决方案。 我是Java的初学者,目前在大学休息期间练习一些代码。

我正在尝试制作电话簿程序。目前我正在尝试添加一个新的联系人,下面是我的代码,但我不知道如何将数据存储在一个数组中,有人可以给我一些指示。

    import java.util.Scanner;

    public class addContact {
    public static void main(String [] args){

    //declare arrays
        String [] contactName = new String [12];
        String [] contactPhone = new String [12];
        String [] contactAdd1 = new String [12];
        String [] contactAdd2 = new String [12];

    //inputs
    String name = "";
    String phone = "";
    String add1 = "";
    String add2 = "";

    //method of taken input
    Scanner input = new Scanner(System.in);

    //while name field is empty display prompt etc.
    while (name.equals(""))
    {
    System.out.println("Enter contacts name: ");
    name = input.nextLine();
    name += contactName[];
    }


    while (add1.equals(""))
    {
    System.out.println("Enter contacts addressline1:");
    add1 = input.nextLine();
    add1 += contactAdd1[];
    }

    while (add2.equals(""))
    {
    System.out.println("Enter contacts addressline2:");
    add2 = input.nextLine();
    add2 += contactAdd2[];
    }

    while (phone.equals(""))
    {
    System.out.println("Enter contact phone number: ");
    phone = input.nextLine();
    phone += contactPhone[];
    }

    }   
}

6 个答案:

答案 0 :(得分:3)

更简洁的方法是创建一个包含PersoncontactName等的contactPhone对象。然后,使用ArrayList而不是数组来添加新的对象。创建一个循环,接受每个`Person:

的所有字段
while (!done) {
   Person person = new Person();
   String name = input.nextLine();
   person.setContactName(name);
   ...

   myPersonList.add(person);
}

使用该列表将不再需要进行数组边界检查。

答案 1 :(得分:2)

此代码的一个问题是:

name += contactName[];

该指令不会在数组中插入任何内容。相反,它会将变量名的当前值与contactName数组的字符串表示形式连接起来。

而是使用它:

contactName[index] = name;

此指令将变量名称存储在索引index的contactName数组中。

您遇到的第二个问题是您没有变量index

你可以做的是一个循环,有12次迭代来填充你的所有数组。 (而index将是你的迭代变量)

答案 2 :(得分:1)

//go through this code I have made several changes in it//

import java.util.Scanner;

public class addContact {
public static void main(String [] args){

//declare arrays
String [] contactName = new String [12];
String [] contactPhone = new String [12];
String [] contactAdd1 = new String [12];
String [] contactAdd2 = new String [12];
int i=0;
String name = "0";
String phone = "0";
String add1 = "0";
String add2 = "0";
//method of taken input
Scanner input = new Scanner(System.in);

//while name field is empty display prompt etc.
while (i<11)
{
    i++;
System.out.println("Enter contacts name: "+ i);
name = input.nextLine();
name += contactName[i];
}


while (i<12)
{
    i++;
System.out.println("Enter contacts addressline1:");
add1 = input.nextLine();
add1 += contactAdd1[i];
}

while (i<12)
{
    i++;
System.out.println("Enter contacts addressline2:");
add2 = input.nextLine();
add2 += contactAdd2[i];
}

while (i<12)
{
    i++;
System.out.println("Enter contact phone number: ");
phone = input.nextLine();
phone += contactPhone[i];
}

}   
}

答案 3 :(得分:0)

这会更好吗?

import java.util.Scanner;

public class Work {

public static void main(String[] args){

    System.out.println("Please enter the following information");

    String name = "0";
    String num = "0";
    String address = "0";

    int i = 0;

    Scanner input = new Scanner(System.in);

    //The Arrays
    String [] contactName = new String [7];
    String [] contactNum = new String [7];
    String [] contactAdd = new String [7];

    //I set these as the Array titles
    contactName[0] = "Name";
    contactNum[0] = "Phone Number";
    contactAdd[0] = "Address";

    //This asks for the information and builds an Array for each
    //i -= i resets i back to 0 so the arrays are not 7,14,21+
    while (i < 6){

        i++;
        System.out.println("Enter contact name." + i);
        name = input.nextLine();
        contactName[i] = name;
    }

    i -= i;
    while (i < 6){
        i++;
        System.out.println("Enter contact number." + i);
        num = input.nextLine();
        contactNum[i] = num;
    }

    i -= i;
    while (i < 6){
        i++;
        System.out.println("Enter contact address." + i);
        num = input.nextLine();
        contactAdd[i] = num;
    }


    //Now lets print out the Arrays
    i -= i;
    while(i < 6){
    i++;
    System.out.print( i + " " + contactName[i] + " / " );
    }

    //These are set to print the array on one line so println will skip a line
    System.out.println();
    i -= i;
    i -= 1;

    while(i < 6){
    i++;

    System.out.print( i + " " + contactNum[i] + " / " );
    }

    System.out.println();
    i -= i;
    i -= 1;

    while(i < 6){
        i++;

    System.out.print( i + " " + contactAdd[i] + " / " );
    }

    System.out.println();

    System.out.println("End of program");

}

}

答案 4 :(得分:0)

如果我错了,请纠正我。

public static void main(String[] args) {

    Scanner na = new Scanner(System.in);
    System.out.println("Please enter the number of contacts: ");
    int num = na.nextInt();

    String[] contactName = new String[num];
    String[] contactPhone = new String[num];
    String[] contactAdd1 = new String[num];
    String[] contactAdd2 = new String[num];

    Scanner input = new Scanner(System.in);

    for (int i = 0; i < num; i++) {

        System.out.println("Enter contacts name: " + (i+1));
        contactName[i] = input.nextLine();

        System.out.println("Enter contacts addressline1: " + (i+1));
        contactAdd1[i] = input.nextLine();

        System.out.println("Enter contacts addressline2: " + (i+1));
        contactAdd2[i] = input.nextLine();

        System.out.println("Enter contact phone number: " + (i+1));
        contactPhone[i] = input.nextLine();

    }

    for (int i = 0; i < num; i++) {
        System.out.println("Contact Name No." + (i+1) + " is "+contactName[i]);
        System.out.println("First Contacts Address No." + (i+1) + " is "+contactAdd1[i]);
        System.out.println("Second Contacts Address No." + (i+1) + " is "+contactAdd2[i]);
        System.out.println("Contact Phone Number No." + (i+1) + " is "+contactPhone[i]);
    }
}

`

答案 5 :(得分:-1)

到目前为止,java中没有使用指针。您可以从类中创建一个对象,并使用彼此链接的不同类,并使用主类中每个类的函数。