简体   繁体   中英

How can i add the static variable number in a method?

I create a ProductMenu class to perform the add, search, delete and update product tasks and it only use the array. What problem that i face is how to add static variable in a method because i failed to add the maximum number of array after add a product into array. Hope can help and thank you.

public static void addProduct(){

    Product product = new Product();
        System.out.println("================================");
        System.out.println("           Add Product          ");
    System.out.println("================================");
    System.out.println("Product ID :- " + Product.getProdId());
    System.out.print("Enter product name: ");
    String prodName = input.next();
    System.out.print("Enter product price: ");
    double prodPrice = input.nextDouble();
    product.setProductId(Product.getProdId());
    product.setProductName(prodName);
    product.setProductPrice((double)prodPrice);

    products[productIndex] = product;
    numOfArray++; // should be add ?
    productIndex++; // should be add also?

    ProductMenu.main(null); //back to main


}

My class

 public class ProductMenu {

  static Scanner input = new Scanner(System.in);
   static int productIndex; //should be add after addProduct()
    static int numOfArray = 1; //should be add after addProduct()
static Product[] products = new Product[numOfArray];

public static void main(String[] args) {

    int menuOption;

    do{

    menu();
    System.out.print("Enter your choice: ");
    menuOption = input.nextInt();

    switch(menuOption){
        case 1:
            addProduct();
            break;
        case 2:
            System.out.print("halo" + products[0].toString());
            break;
        case 3:

            break;
        case 4:

            break;
        default:

            System.out.print("Invalid option. Please try 
                    again.\n");
            break;
    }

    }while(menuOption!=1 && menuOption!=2 && menuOption!=3 && 
      menuOption!=4 );

}

    public static void menu(){
        System.out.println("================================");
        System.out.println("          Product Menu         ");
    System.out.println("================================");
    System.out.println("1. Insert Product\n2. Update Product\n3. 
        Search Product\n4. Delete Product\n");
}

This is error msg that mean array number already more than limit and why i ask if i not wrong.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at ProductMenu.addProduct(ProductMenu.java:64)
at ProductMenu.main(ProductMenu.java:21)
at ProductMenu.addProduct(ProductMenu.java:68)
at ProductMenu.main(ProductMenu.java:21)

I was wondering whether you could replace array with arraylist in your code. Arraylists are backed by arrays in background and you need not worry about arrayindexoutofbound exception while handling the data

To your problem: I would use an ArrayList or a LinkedList because it has a dynamic size. The size of an array cannot be changed after initialization and yours is set to 1. That will cause problems.

Addition: You do not need to call ProductMenu.main(null); //back to main ProductMenu.main(null); //back to main because your addProduct function is called in the main method and after executing your function your program will continue in the main function.

As you need to use Array only then create one function add() which will first check size of array, if there is space left then add element else create new array of double size , copy all previous elements and add new one at end. Also as size is static variable you don't have to pass to your method. Your method is also static and both method and variable in same class so you can directly use variable in method.

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