Question: I need java help and can't seem to find any straightforward answers online. I am creating a menu with different methods and different tasks. One
I need java help and can't seem to find any straightforward answers online.
I am creating a menu with different methods and different tasks. One of the tasks is use a method and add a product (one if else statement) and view products (another if else statement) I am using the same variable for both, trying to receive the input and have it written back into the output of the console, but it just says "null". Below is the pasted portion of my code in which I am having trouble with. Choice 2 is what I am having trouble with as the "product[i]" isn't working there. If you see anything wrong with it please help!!!
String[] product = new String [20]; // products // establishing variables int choice; int i = 0; // creating while loop while(true) { // invalid choice System.out.print("Enter your choice: "); choice = input.nextInt(); if(choice<1 || choice>2){ System.out.println("Invalid choice."); } // choice 1 if(choice ==1){ System.out.print("Please enter product name:"); product[i] = input.next(); // adding product System.out.println(" Added Successfully:" + (product[i])); break; } else if // choice 2 (choice ==2){ // viewing inventory System.out.println(" Inventory Products"); System.out.println(product[i]); break; }} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
