【问题标题】:Using a scanner to accept String input and storing in a String Array使用扫描仪接受字符串输入并存储在字符串数组中
【发布时间】:2012-12-26 03:15:45
【问题描述】:

谁能帮帮我。我进行了多次搜索,但在任何地方都找不到解决方案。 我是 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[];
    }

    }   
}

【问题讨论】:

    标签: java arrays string java.util.scanner


    【解决方案1】:

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

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

    使用列表将消除对数组边界检查的需要。

    【讨论】:

    • 感谢您的回复。我从未使用过数组列表,这就是为什么我有单独的数组用于联系详细信息。我现在将研究使用数组列表。
    【解决方案2】:

    这段代码的问题之一是:

    name += contactName[];
    

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

    改用这个:

    contactName[index] = name;
    

    这条指令会将变量名存储在索引index处的contactName数组中。

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

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

    【讨论】:

      【解决方案3】:
      //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];
      }
      
      }   
      }
      

      【讨论】:

        【解决方案4】:

        这样会更好吗?

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

        【讨论】:

        • 您应该真正解释为什么它会更好地工作以及它有何不同。这是很多要筛选的代码;大多数人只发布显着差异。
        【解决方案5】:

        如果我错了,请纠正我。`

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

        `

        【讨论】:

          【解决方案6】:

          到目前为止,java 中还没有使用指针。您可以从类中创建一个对象,并使用相互链接的不同类,并使用主类中每个类的功能。

          【讨论】:

            猜你喜欢
            • 2011-08-23
            • 1970-01-01
            • 1970-01-01
            • 2014-12-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多