shouldn't the else-if statement execute if and only if the if statement is false?
However, the vector is empty and as I proceed to input the first elemnet, the first if statement works just fine, but then the else-if statement is executed and hangs in an infinite loop.
public void addProduct (Vector <Stock> temp){
Scanner sc = new Scanner (System.in);
sc.useDelimiter("\n");
String name = "";
String qty = "";
String cost = "";
int q = 0;
int c = 0;
boolean v;
boolean added = false;
Stock st = null;
System.out.println("Enter Product Name: ");
name = sc.next();
do{
if((name.matches(valn)) && (temp.size() == 0)){
v = true;
do{
try{
System.out.println("Enter Quantity: ");
q = sc.nextInt();
System.out.println("Enter Cost: ");
c = sc.nextInt();
v = false;
}catch(InputMismatchException ie){
sc.next();
System.out.println ("Invalid Entry!");
}
}while(v);
qty = Integer.toString(q);
cost = Integer.toString(c);
Stock s1 = new Stock(name, qty, cost);
temp.add(s1);
added = true;
System.out.println ("Product was Added Succesfully!");
System.out.println ();
}
else if((name.matches(valn)) && (temp.size() > 0)){
for(int i = 0; i < temp.size(); i ++){
st = temp.elementAt(i);
if(name == st.getName()){
System.out.println ("Product Already Exists.");
System.out.println ();
System.out.println ("Note: Product will be Overwritten!");
v = true;
do{
try{
System.out.println("Enter Quantity: ");
q = sc.nextInt();
System.out.println("Enter Cost: ");
c = sc.nextInt();
v = false;
}catch(InputMismatchException ie){
sc.next();
System.out.println ("Invalid Entry! Please Try Again.");
}
if((qty.matches(valq)) && (cost.matches(valc))){
qty = Integer.toString(q);
cost = Integer.toString(c);
temp.elementAt(i).setQuantity(qty);
temp.elementAt(i).setCost(cost);
}
}while(v);
added = true;
}
System.out.println ();
}
}else
System.out.println ("Invalid Name Input!");
}while(added == true);
System.out.println ();
}`
Aucun commentaire:
Enregistrer un commentaire