so I'm making this array operations menu and I'm having problems on my first option which is to "create an array". I'm supposed to input the size of the array here, but if it's < 5 or > 20 it will send an error message, that I can do. But I'm also supposed to to show an error message if an array is already created. How can I do that? thanks for the response.
class process{
Scanner in = new Scanner(System.in);
int choice1 =0;
int limit1;
int array[];
public void m(){
System.out.println(" ARRAY OPERATION");
System.out.println(" menu");
System.out.println(" [1] Create Array");
System.out.println(" [2] Insert Element");
System.out.println(" [3] Search");
System.out.println(" [4] Display");
System.out.println(" [5] Delete");
System.out.println(" [0] Stop");
System.out.print(" Enter choice:");
e();
}
public void e(){
choice1 = in.nextInt();
cls();
switch(choice1){
case 1:
{
System.out.println("Create Array");
System.out.println("Enter the limit of your array: ");
limit1 = in.nextInt();
if(limit1 <5){
System.out.println("Error: Minimum limit exceeded");
System.out.println("Going back to main menu");
m(); //loop to main menu
}
else if(limit1 >20){
System.out.println("Error: Maximum limit exceeded");
System.out.println("Going back to main menu");
m(); //loop to main menu
}
else if(limit1 >=5 || limit1 <=20){
System.out.println("An array with a limit of " + limit1 + " has been created");
array = new int[limit1];
m();
}
}
add in case 1:
if (array != null) {
System.out.println("Already exists");
break;
}