Sarmad Aijaz Sarmad Aijaz - 1 month ago 6
Java Question

editing in arraylist with multiple strings

I have made a program...
in which I scan 3 different strings and then convert them into a single string using toString() then I put that string into array list
for example
"name phonenumber addrees"
it works fine....the problem is when i have to edit it...
i use string.split to split them and edit them...
but i dont know what is wrong..whenever i try to edit it goes to exception error...can anyone help me out?
PROBLEM IN CASE 3 of first switch

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

}}


my toString method--->

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

}


}

Answer

The problem in this code was the choice object.. Because if the input is a wrong input,choice object stays like that forever that's there should be two objects for the input

Comments