我正在扫描三个不同的输入,并使用toString
将它们转换为单个字符串。然后我想编辑各个输入
例如:
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);
}
}
答案 0 :(得分: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.
}
答案 1 :(得分: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;
}
}
我还无法测试,所以请随时询问是否存在问题。