简体   繁体   English

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

[英]Using a scanner to accept String input and storing in a String Array

Can someone help me please. 有谁可以帮助我吗。 I have done numerous searches but can't find a solution anywhere. 我做了很多搜索,但无法在任何地方找到解决方案。 I'm a beginner to Java and currently practicing some code while on a break from college. 我是Java的初学者,目前在大学休息期间练习一些代码。

I am trying to make a Phonebook program. 我正在尝试制作电话簿程序。 At the moment I am trying to add a new contact, below is the code i have but i am not sure how to store the information in an array can someone give me some pointers please. 目前我正在尝试添加一个新的联系人,下面是我的代码,但我不知道如何将数据存储在一个数组中,有人可以给我一些指示。

    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[];
    }

    }   
}

A cleaner approach would be to create a Person object that contains contactName , contactPhone , etc. Then, use an ArrayList rather then an array to add the new objects. 一个清洁的方法是创建一个Person ,其中包含对象contactNamecontactPhone等。然后,使用一个ArrayList而不是一个数组中添加新的对象。 Create a loop that accepts all the fields for each `Person: 创建一个接受每个`Person的所有字段的循环:

while (!done) {
   Person person = new Person();
   String name = input.nextLine();
   person.setContactName(name);
   ...

   myPersonList.add(person);
}

Using the list will remove the need for array bounds checking. 使用该列表将不再需要数组边界检查。

One of the problem with this code is here : 这段代码的一个问题是:

name += contactName[];

This instruction won't insert anything in the array. 该指令不会在数组中插入任何内容。 Instead it will concatenate the current value of the variable name with the string representation of the contactName array. 相反,它将连接变量名称的当前值与contactName数组的字符串表示形式。

Instead use this: 而是使用这个:

contactName[index] = name;

this instruction will store the variable name in the contactName array at the index index . 此指令将变量名称存储在索引index的contactName数组中。

The second problem you have is that you don't have the variable index . 你遇到的第二个问题是你没有变量index

What you can do is a loop with 12 iterations to fill all your arrays. 你可以做的是一个循环,有12次迭代来填充你的所有数组。 (and index will be your iteration variable) index将是你的迭代变量)

//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];
}

}   
}

Would this work better? 这会更好吗?

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");

}

}

Please correct me if I'm wrong.` 如果我错了,请纠正我

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]);
    }
}

` `

There is no use of pointers in java so far. 到目前为止,java中没有使用指针。 You can create an object from the class and use different classes which are linked with each other and use the functions of every class in main class. 您可以从类中创建一个对象,并使用彼此链接的不同类,并使用主类中每个类的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM