在arraylist中编辑多个字符串

时间:2016-02-17 15:11:41

标签: java arraylist

我制作了一个节目...... 我在其中扫描3个不同的字符串然后使用toString()将它们转换为单个字符串然后我将该字符串放入数组列表中 例如 “名称phonenumber addrees” 它工作正常....问题是当我必须编辑它... 我使用string.split来分割它们并编辑它们...... 但我不知道出了什么问题。每当我尝试编辑它就会出现异常错误......任何人都可以帮助我吗? 第一个开关的情况3中的问题

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

public class mainClass {

public static void main(String[] args) {
    Scanner input= new Scanner(System.in);
    ArrayList<String> arraylist= new ArrayList<String>();
    CreateFormat FormatObject = new CreateFormat();

    int choice;
    String phoneNumber;
    String name,address;
    String format="Empty";
    int x=1;
    int flag=0;



    do
    {

    try

    {   
    System.out.println("Enter your choice");
    System.out.printf("1:Enter new data\n2:Display data");
    choice=Integer.parseInt(input.next());
    switch (choice)
    {
    case 1:
    {
        System.out.println("Enter name  ");
        name=input.next();
        System.out.println("Enter phone number");
        phoneNumber=input.next();
        System.out.println("Enter address");
        address=input.next();
        format=FormatObject.toString(phoneNumber, name, address); // will merge these 3 Strings with space in between
        arraylist.add(format);
        flag++;


    }
        break;
    case 2:
    {
        System.out.println("Name   Phone number   Address");
        System.out.println();
        for(int i=0;i<flag;i++)
        {   
        System.out.println(arraylist.get(i)); //arraylist cant be displayed on nextline without loop

        }
    }
        break;
    case 3:
        {
        System.out.println("Enter the position you want to edit");
        System.out.println("1:Name\n2:Phone number\n3:Address");
        int choice2;
        choice2=input.nextInt();
        String dupFormat= arraylist.get(choice2-1);
        String[] splitString= dupFormat.split(" ");
         switch (choice2)
         {
         case 1:
         {   System.out.println("Enter new name");
             splitString[0]=input.next();
             break;
         }
         case 2:
         {   System.out.println("Enter new phone number");
             splitString[1]=input.next();
         }
             break;
         case 3:
         {   System.out.println("Enter new Address");
             splitString[2]=input.next();
         }
         break;
         default:
         {   
            System.out.println("Choice is only 1,2,3 ");

         }

         String newFormat=splitString.toString();
         arraylist.add(choice2-1, newFormat);
         }


        }break;

    default:
        System.out.println("Error");
        break;


    }

}

catch(Exception InputMismatchException){
System.out.println("Enter right choice ..");    
}
    }while(x==1);

}}

我的toString方法---&gt;

public class CreateFormat {
String phoneNumber;
String nameUser;
String addressUser;
public String toString(){

return String.format("%s %s %s", nameUser,phoneNumber,addressUser);
}
public String toString (String phone,String name,String address){
phoneNumber=phone;
nameUser=name;
addressUser=address;
return String.format("%s %s %s", nameUser,phoneNumber,addressUser);

}

}

3 个答案:

答案 0 :(得分:0)

您的第二个交换机案例之外有break;个声明。例如,您在

之外有break;
case 2:
     {   System.out.println("Enter new phone number");
         splitString[1]=input.next();
     }
         break;

它会破坏第一个开关。

所以新字符串不会添加到数组中。即使你打电话给这个

String newFormat=splitString.toString();
arraylist.add(choice2-1, newFormat);

它会将您的数据更改为[Ljava.lang.String;@1f7030a6之类的内容。这是因为您尝试从toString()调用新创建的数组上的dupFormat.split(" ")

我推荐将变更案例3部分推荐给

System.out.println("Enter the position you want to edit");
System.out.println("1:Name\n2:Phone number\n3:Address");
int choice2;
choice2 = input.nextInt();
String dupFormat = arraylist.get(choice2 - 1);
String[] splitString = dupFormat.split(" ");
switch (choice2) {
    case 1: {
        System.out.println("Enter new name");
        FormatObject.nameUser = input.next();
        break;
    }
    case 2: {
        System.out.println("Enter new phone number");
        FormatObject.phoneNumber = input.next();
        break;
    }
    case 3: {
        System.out.println("Enter new Address");
        FormatObject.addressUser = input.next();
        break;
    }
    default: {
        System.out.println("Choice is only 1,2,3 ");
    }
}
String newFormat = FormatObject.toString();
arraylist.add(choice2 - 1, newFormat);
break;

您将更改对象的状态,之后您可以使用新数据再次致电toString()

答案 1 :(得分:0)

无法找到问题,但您的代码很难阅读.. 为什么要将姓名,电话号码和地址存储为字符串?

我建议:

  1. 创建包含这些字段的类。
  2. 创建一个函数来实现你在每种情况下的代码(这样你也可以分别测试每个函数)
  3. 函数中的
  4. 与(1)中的类的实例一起使用,并且仅当您要打印时才转换为字符串。
  5. 我也会读到do-while,switch,但这取决于你
  6. 在编码时也会尝试提供更有意义的名称。例如,您的标志用作计数而不是标志。
  7. 所以你应该这样做:(并单独测试每个功能)

        private List<PersonalData> list = new ArrayList<>();
    
        ///....
    
        switch(choise){
            case 1://consider replacing with enum
            {
                list.add(getPersonalDataFromInput());
                break;
            }
            case 2:
            {
                printAll();
                break;
            }
            case 3:
            {
                System.out.println("Enter the position you want to edit");
                System.out.println("1:Name\n2:Phone number\n3:Address");
                int choice2;
                choice2=input.nextInt();
                String dupFormat= arraylist.get(choice2-1);
                if(choice2==1){
                    list.get(list.size()-1).setName(input.next());
                }
                else if(choice2==2){
                    list.get(list.size()-1).setPhone(input.next());
                }
                else (choice2==3){
                    list.get(list.size()-1).setAddress(input.next());
                }
            }
    
        }
    }
    

    和要创建的类:

    class PersonalData{
            private String phoneNumber;
            private String name;
            private String address;
            //getters and setters
        }
    

答案 2 :(得分:0)

此代码中的问题是choice对象.. 因为如果输入是错误的输入,那么选择对象将永远保持这样 那个输入应该有两个对象