简体   繁体   中英

How can I have a common variable between two methods?

boolean exists;
public void searchProduct(Vector <Stock> temp){
    Scanner sc = new Scanner (System.in);
    sc.useDelimiter ("\n");
    exists = false;
    System.out.println ("Enter Product Name to Search for: ");
    String n = sc.next();
    System.out.println ();
    Stock s = null;
    if(temp.size() == 0)
        System.out.println ("Database is Empty! Please Add a Product.");
    else if (!n.matches(valn))
        System.out.println ("Invalid Product.Please Try Again.");
    else{
        for(int i = 0; i < temp.size(); i ++){
            s = temp.elementAt(i);
            if(s.getName().equalsIgnoreCase(n)){
                System.out.println(s.ToString());
                System.out.println ("Location: " + (i + 1));
                exists = true;
            }
        }
        if(exists != true)
            System.out.println ("Product Not Found!");
        System.out.println ("---------------------------------------------"); 
    } 
}

public void delProduct(Vector <Stock> temp){
    Scanner sc = new Scanner (System.in);
    if(temp.size() == 0)
        System.out.println ("Database is Empty! Please Add a Product.");
    else{
        sc.useDelimiter("\n");
        Stock s = new Stock();
        int choice = 0;
        int i = 0;
        boolean quit = false;
        boolean valid = true; 
        s.searchProduct(temp);
        if(exists == true){ 
            do{
                try{
                    System.out.println ("Enter Location of Product to delete: ");
                    i = sc.nextInt();
                    temp.removeElementAt(i-1);
                    System.out.println ();
                    System.out.println ("Product was Deleted Succesfully!");
                    System.out.println ("--------------------------------"); 
                    valid = false;
                }catch(InputMismatchException ime){
                    sc.next();
                    System.out.println ("Invalid Location.");
                }catch(ArrayIndexOutOfBoundsException a){
                    System.out.println ("Location does not Exist!");
                }
            }while(valid); 
        }
    }
}

As it is, everything works perfectly fine. But the thing is, that if the user enters a product name to delete that does not exist in the vector, he is still expected to enter the location to delete. (Obviously, it is doing what it is supposed to do). I have tried declaring different boolean variables that are set to true if the search item is found, and the delete procedure is only executed if the boolean is true, but somehow the variable is always false in the delete method. Any ideas please? Btw, sorry if I make silly mistakes, I am still new to Java.

The problem is that, when you do s.searchProduct(temp) you are expecting that exists to be true if the product is found but it is not the case. Because while you are doing s.searchProduct(temp) , you are calling method searchProduct(temp) from another Stock object that you called s . Therefore if the product exists in temp, then exists of s will be true, so s.exists will be true. However, what you want to be true is this.exists . For this, you have to replace s.searchProduct(temp) line with this.searchProduct(temp) or searchProduct(temp) .

Also as far as I understand from your code, in delProduct method you don't need to create a Stock object that you called s .

Furthermore, you can also do this delete job by first calling a selectProduct(temp) method which will return the location of the product. If the product doesn't exist, then it will return -1. Then, if the result of searchProduct(temp) method is not -1, use the returned location in a deleteProduct(temp) method. In this case, you won't bother with boolean flags to determine what to do when. Just another idea.

Good luck.

When you call s.searchProduct(temp), exists should become true or false based on whether it is found. Have you verified that this is happening? What is that valn variable you have in there?

Once that's working, you need to add a check within delete to check if exists is true and then ask for the location, otherwise to ask for more input (and then search again).

Have you tried declaring the variable exists as volatile or maybe static so that it shall remain global for the entire class. But it will have to accessed using the classname.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM