0

I am trying to clean my code up by creating a class specifically for an array of information. It is basically like a storage for variables in case I need them later. Here is what I have so far:

package com.input;

import java.util.Scanner;


public class Gender extends Welcome {
    Scanner input = new Scanner(System.in);
    private static String gender;


    public static void setGender() {
        Scanner input = new Scanner(System.in);
        int[] storeInts = new int[25];
        storeInts[0] = 0;
        //The [0] index of array storeInformation is the gender value. 0 = female; 1 = male
        gender = input.nextLine();
        if(gender.equalsIgnoreCase("boy")) {
            System.out.println("What is your name, sir?");
            while (storeInts[0] < 1) {
                storeInts[0]++;
            }
        }else if(gender.equalsIgnoreCase("girl")) {
            System.out.println("What is your name, ma'am?");
        }else{
            System.out.println("You have failed to answer correctly. Try again:");
            init();
        }
        Name nameObject = new Name();
        nameObject.setName(storeInts[0]);
    }

    public static void nextName(int x) {
        if(x == 1) {
            System.out.println("What is your name, sir?");
        }else{
            System.out.println("What is your name, ma'am?");
        }

        Name nameObject = new Name();
        nameObject.setName2();
    }
}

What I'm trying to accomplish here, is if the user types "boy" my code will store 1 in the index [0] of array storeInts[]. If the user types "girl" the index [0] will remain the value of 0.

If I need to refer to the user's gender later on, I want to be able to go back and figure out if they are a "boy" or a "girl" using the array.

I want to be able to called this array from any method within my code. I have already used this array in a complicated way and I would like to find a solution to make it easier.

Here is when I used it:

nameObject.setName(storeInts[0]);

I transferred the index [0] to the setName() method. Here is the setName() method:

 public void setName(int x) {
    String name;
    name = input.nextLine();
    String storedStrings[] = new String[25];
    storedStrings[0] = name;
    FirstTask firstTaskObject = new FirstTask();

    if (name.length() == 0) {
        System.out.println("You must be a unicorn. You want to play games?");
        altInit(x);
    }else{
        System.out.println("Nice to meet you, " + name + "!");
        firstTaskObject.beginning(name);
    }
}

As you can see I created another array in the same manner as the previous one, but this one is to store Strings instead. Now back to what I was saying-- the parameter (int x) is the same value as storeInts[0]. This will tell me if the user is male or female. This value is sent to altInit() method when the user decides to try to continue without typing their name in first.

Here is the altInit() method:

public void altInit(int x) {
    String yesOrNo;
    AltStory altStoryObject = new AltStory();
    Gender backToGender = new Gender();
    yesOrNo = input.nextLine();
    if(yesOrNo.equalsIgnoreCase("yes")) {
        altStoryObject.AltInit();
    }else if(yesOrNo.equalsIgnoreCase("no")) {
        System.out.println("Consider this your last warning...");
        backToGender.nextName(x);
    }else{
        System.out.println("You have failed to answer correctly. Try again:");
        init();
    }
}

When asked if they want to play games, they can type "yes" or "no." If the user types "no" as in they do not want to play games, then the program will print, "Consider this your last warning..." and then continue to the nextName() method in the previous class Gender. This also passes on that index[0] again in the array storedInts[].

Here is the nextName() method:

public static void nextName(int x) {
    if(x == 1) {
        System.out.println("What is your name, sir?");
    }else{
        System.out.println("What is your name, ma'am?");
    }

    Name nameObject = new Name();
    nameObject.setName2();
}

As you can see, if the user is that value of a male (or 1) then the program will print, "What is your name, sir?." If the value is a female (or 0), then the program will print, "What is your name, ma'am?"

This whole time I felt like the stored value of storeInts[0], was just leap frogging around until it was used... I want to prevent this by just creating a class with methods giving me the ability to call any value stored in that array whenever I need it. How do I create an array, store it in a method, and call it when needed?

As someone has requested, here is the entire code:

//Gender class
package com.input;

import java.util.Scanner;


public class Gender extends Welcome {
Scanner input = new Scanner(System.in);
private static String gender;


public void setGender() {
    Scanner input = new Scanner(System.in);
    int [] storeInts = new int[25];
    storeInts[0] = 0;
    //The [0] index of array storeInformation is the gender value. 0 = female; 1 = male
    gender = input.nextLine();
    if (gender.equalsIgnoreCase("boy")) {
        System.out.println("What is your name, sir?");
        while(storeInts[0]<1){
            storeInts[0]++;
        }
    } else if (gender.equalsIgnoreCase("girl")) {
        System.out.println("What is your name, ma'am?");
    } else {
        System.out.println("You have failed to answer correctly. Try again:");
        init();
    }
    Name nameObject = new Name();
    nameObject.setName(storeInts[0]);
}

public void nextName(int x){
    if (x == 1) {
        System.out.println("What is your name, sir?");
    }else {
        System.out.println("What is your name, ma'am?");
    }

    Name nameObject = new Name();
    nameObject.setName2();
    }

}
//Name class
package com.input;

public class Name extends Gender{

public void setName(int x) {
    String name;
    name = input.nextLine();
    String storedStrings[] = new String[25];
    storedStrings[0] = name;
    FirstTask firstTaskObject = new FirstTask();

    if (name.length() == 0) {
        System.out.println("You must be a unicorn. You want to play games?");
        altInit(x);
    } else {
        System.out.println("Nice to meet you, " + name + "!");
        firstTaskObject.beginning(name);
    }
}

public void altInit(int x){
    String yesOrNo;
    AltStory altStoryObject = new AltStory();
    Gender backToGender = new Gender();
    yesOrNo = input.nextLine();
        if(yesOrNo.equalsIgnoreCase("yes")) {
            altStoryObject.AltInit();
        }else if(yesOrNo.equalsIgnoreCase("no")){
            System.out.println("Consider this your last warning...");
            backToGender.nextName(x);
        }else{
            System.out.println("You have failed to answer correctly. Try again:");
            init();
        }
}
public void setName2() {
    String name;
    name = input.nextLine();
    FirstTask firstTaskObject = new FirstTask();

    if (name.length() == 0) {
        System.out.println("You have failed to answer correctly. Try again:");
        init();
    } else {
        System.out.println("Nice to meet you, " + name + "!");
        firstTaskObject.beginning(name);
        }
    }
}

How do I create an array, store it in a method, and call it when needed?

  • 3
    **Please** don't make all of your methods `static`, **please** post a complete code example, and **please** ask an actual question. – Elliott Frisch Jan 26 '19 at 07:00
  • It sounds like you are overcomplicating things. Elliot Frisch is correct in that making everything static defeats the purpose of object orientation. Also, in your first paragraph, you seem to be describing a bean: https://stackoverflow.com/q/3295496 – TCCV Jan 26 '19 at 07:11
  • You could have a look at design patterns, there you will find a suitable approach – BlackFlag Jan 26 '19 at 07:27
  • I have included the entire source code at the end. I have also stopped some methods from being static. And I included my question... – Micah Wright Jan 26 '19 at 09:12

0 Answers0