0
class CreateArray{ 
Scanner input = new Scanner(System.in);

public void Create(){
System.out.print("How many numbers numbers do you want to enter? Minimum of 5 and a maximum of 20 only: ");
    int num = input.nextInt();
    if (num >=5 || num >20){
        int array[] = new int[num];
        System.out.println("Enter the numbers you want: ");
        if(array.length != 0){
        for(int i = 0 ; i<array.length; i++){
            array[i] = input.nextInt();
        
        }
    }
        System.out.print("Your array of numbers are: ");
        for(int i = 0 ; i<array.length; i++){
            System.out.print(array[i] + " ");
        }
    }
    else{
        System.out.print("Please input a the right numbers of array");
    
    }   }}

I would like to know how to identify if array is already created so that i can display an error message. I have two classes as you can see above theres the class CreateArray and here is the main class: I am new to java actually so forgive me. And also the logic is that when user create an array then they decide to continue and check again the code will output "you have already created an array" Thank you so much for answering.

public class Lab3

{public static void main(String[] args){
    
    Scanner ans = new Scanner(System.in);
    String choice;
    String choices;
    do{
        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("\nEnter Choice: ");
        choice = ans.nextLine();
        if(choice.equals("1")){
            CreateArray myObj = new CreateArray();
            myObj.Create();
    }
    else{
        System.out.print("Array has been created please procedd to other options!");
    }
    System.out.println();
    System.out.print("Do you want to continue? : ");
    choices =ans.nextLine();
}
while(!choices.equals("-1") || !choices.equals("-1"));
}}
Cid Moto
  • 1
  • 1
  • Please fix the indentation of your code. If you're using an IDE you can have it format the code for you, quite possibly by typing Ctrl-F. – David Conrad Oct 22 '20 at 23:27
  • 1
    sorry about that kinda new to programming. – Cid Moto Oct 22 '20 at 23:30
  • The array needs to be a [global variable](https://stackoverflow.com/questions/4646577/global-variables-in-java) like this `public static int array[]` so that you can access it from the main class and check if it has already been created `if(choice.equals("1") && myObj.array != null) {System.out.print("Array has been created please procedd to other options!");}` – sorifiend Oct 23 '20 at 00:05
  • How can i make a global variable out of it? im really sorry for bugging you im new to this – Cid Moto Oct 23 '20 at 00:08
  • Check the link in my last comment: https://stackoverflow.com/questions/4646577/global-variables-in-java You need to change `int array[] = new int[num];` to `array = new int[num];`, and you need to add `public static int array[]` to the line below `Scanner input = new Scanner(System.in);` – sorifiend Oct 23 '20 at 00:09
  • 1
    Thank you so so much that help me a lot thankssss May god bless you – Cid Moto Oct 23 '20 at 00:28
  • Note: you want `if (num >=5 && num <= 20)` – Scary Wombat Oct 23 '20 at 00:53
  • @sorifiend please consider combining your comments into an answer – Paul Oct 23 '20 at 11:33

1 Answers1

0

You can check if an array is already created by using a global array variable that you can check from your main class.

Add this line int array[]; to the CreateArray class as a global variable, and replace int array[] = new int[num]; with array = new int[num]; so that it referances to the global vairable.

Then from your main/Lab3 class you can simply use if (myObj.array == null) to check if the array has NOT been created, or use if (myObj.array != null) to check if the array HAS been created.

Note, you also need to place the following code outside of the do{} while () loop CreateArray myObj = new CreateArray(); otherwise it will create a new object every loop and you will not be able to keep the inputs.

The complete code might look like this (with a few other changes to make more sense):

class CreateArray {

    Scanner input = new Scanner(System.in);
    //place a global variable here
    int array[];

    public void Create() {
        System.out.print("How many numbers numbers do you want to enter? Minimum of 5 and a maximum of 20 only: ");
        int num = input.nextInt();
        //Fix this line as shown by Scary Wombat in comments:
        if (num <= 5 || num <= 20) {
            //Initialize the array here (This will make it a non null value)
            array = new int[num];
            System.out.println("Enter the numbers you want: ");
            if (array.length != 0) {
                for (int i = 0; i < array.length; i++) {
                    array[i] = input.nextInt();
                }
            }
            System.out.print("Your array of numbers are: ");
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + " ");
            }
        } else {
            System.out.print("Please input a the right numbers of array");
        }
    }
}

And the Lab3 class could look like this:

public class Lab3 {

    public static void main(String[] args) {

        Scanner ans = new Scanner(System.in);
        String choice;
        String choices;
        //Place the CreateArray object here so that it can be re-used between selecting options, otherwise you will lose all progress
        CreateArray myObj = new CreateArray();
        do {
            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("\nEnter Choice: ");
            choice = ans.nextLine();
            //Only call this method if it has not been created yet
            if (choice.equals("1") && myObj.array == null) {
                myObj.Create();
            }
            //If another option is chosen but the array has not been created, then give an error message
            else if (myObj.array == null) {
                System.out.print("Array has not been created yet, please choose option 1.");
            }
            else if (choice.equals("1")) {
                System.out.print("THe array has already been created, please choose another option.");
            } 
            //Place all other value checkes after the above checks:
            else if (choice.equals("2")) {
                System.out.print("This opetion is not yet supported.");
            } else if (choice.equals("3")) {
                System.out.print("This opetion is not yet supported.");
            } else if (choice.equals("4")) {
                System.out.print("This opetion is not yet supported.");
            } else if (choice.equals("5")) {
                System.out.print("This opetion is not yet supported.");
            } else if (choice.equals("0")) {
                System.out.print("This opetion is not yet supported.");
                break;
            } else {
                System.out.print("Invalid option.");
            }
            System.out.println();
            System.out.print("Do you want to continue? : ");
            //What does this do?
            choices = ans.nextLine();
        } while (!choices.equals("-1"));
    }
}
sorifiend
  • 5,927
  • 1
  • 28
  • 45