2016-02-16 158 views
0

我正在掃描三個不同的輸入並使用toString將它們轉換爲單個字符串。然後我想編輯個別輸入。
例如:從arraylist中提取組合字符串

name phoneNumber address 
sarmad 12345 myhouse 

如何編輯 '的​​MyHouse'?

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

        default:{ 
         System.out.println("Enter right choice"); 
        } 
       } 
      } 

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

toString方法:

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

回答

1

你所需要的就是實現setters/getters你的屬性。

public class CreateFormat { 
private String phoneNumber; 
private String nameUser; 
private String addressUser; 

public void setPhoneNumber(String phoneNumber) { 
    this.phoneNumber = phoneNumber; 
} 

public String getPhoneNumber() { 
    return phoneNumber; 
} 
// Similar set & get methods for other properties too. 
} 
0

我允許我自己對代碼進行一些更改,因爲我的理解是,放棄String並使用Object本身反而更容易和窒息。

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

public class mainClass { 

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

     int choice; 
     String phoneNumber; 
     String name; //sepperated for readability 
     String address; 
     //String format="Empty"; not used anymore 
     int x = 1; 
     //int flag = 0; is not necessary 
     do{ 
      try{ 
       System.out.println("Enter your choice"); 
       System.out.printf("1:Enter new data\n2:Display data"); 
       choice = input.nextInt();//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(); 
         arraylist.add(new CreateFormat(name, phoneNumber, address)); //changed to an object of type CreateFormat instead of String 
         //flag++; not necessary 
        } 
         break; 

        case 2:{ 
         //System.out.println("Name Phone number Address"); 
         //System.out.println(); 
         for(int i=0;i<arrayList.size();i++){// size = method from the ArrayList library 
          System.out.println("Name:" + arrayList.get(i).getNameUser()); 
          System.out.println("Phone Number:" + arrayList.get(i).getPhoneNumber()); 
          System.out.println("Address:" + arrayList.get(i).getAddressUser()); 
          //System.out.println(arraylist.get(i)); 
         } 
        } 
        break; 

        default:{ 
         System.out.println("Enter right choice"); 
        } 
       } 
      } 

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

創建格式:

public class CreateFormat { 

    String phoneNumber; 
    String nameUser; 
    String addressUser; 

    public CreateFormat(String phoneNumber, String nameUser, String addressUser){ 
     this.phoneNumber = phoneNumber; 
     this.nameUser = nameUser; 
     this.addressUser = addressUser; 
    } 

    public String getPhoneNumber(){ 
     return this.phoneNumber; 
    } 

    public String getNameUser(){ 
     return this.nameUser; 
    } 

    public String getAddressUser(){ 
     return this.addressUser; 
    } 
} 

我無法測試它,可以隨意問,如果有一些問題。