Hi all,
I have created the following code that deletes a Vector entry. Currently it gives an error if you enter a string that is not present in the Vector. Can anyone recomend a way of looping back to the original text entry point, rather than terminating the program if an incorrect entry is made??
import java.util.*;
public class Q5
{
public static void main(String[] args)
{
Vector<String> staffList = new Vector<String>();
String[] staff = {"John Smith",
"Sally Ann Jones",
"James Paul Black"};
String locate;
int Index;
Scanner keyboard = new Scanner(System.in);
for (int i=0; i<staff.length; i++)
staffList.add(staff[i]);
for (int i=0; i<staffList.size(); i++)
{
String person =
staffList.elementAt(i); //No typecast!
System.out.println("Staff member "
+ (i+1) + ": " + person);
}
System.out.println("Please enter a name to be removed from the list: ");
locate=keyboard.nextLine();
keyboard.close();
Index = staffList.indexOf(locate);
if (Index >=0)
{
staffList.removeElementAt(Index);
for(int i=0; i<staffList.size(); i++)
{
String person =
staffList.elementAt(i);
System.out.println("Staff member "
+ (i+1) + ": " + person);
}
}
else
System.out.print("Error: Please enter a valid name! ");
}
}
Much Apreciated.