0

I've made a bit of a mess on a project of mine. I have to create an array of objects. I have made an array but it only has 1 field 'myMonths' referring to the length of time of the project. In my main method:

case 1:

    int n = 1;    //int n = number of projects
    Scanner sc = new Scanner(System.in);

    //myMonths = new int[amount];

    System.out.println("** Only projects with a duration between 2 and 12 months can be included **");
    System.out.println("What was the duration of your projects in months?");

    for(int i=0; i<1; i++){
        int a = sc.nextInt();

        //display error message
        if((a < 2) || (a > 12)){
            System.out.println(" Please enter an amount between 2 and 12 months. ");
        }

        //add to the array
        else{
            myMonths[index++] = a;
        }
    }

    calc.setMyMonths(myMonths); //creating the object
    break;

In my class on separate file:

public class MenuTestClass{

private int myMonths[];
private double average; //store average value of numbers
private boolean averageCanBeCalculated;
private int max; // store max number from array. to be calculated

public MenuTestClass(){
    myMonths = new int[5];
    }

 public MenuTestClass(int[] myMonths){
   this.myMonths = myMonths;
   }
 public void setMyMonths(int[] values){ //declare setter method
   myMonths = values;
   }

I should have added in two more fields, both strings. Is there a way I can add more fields/attributes to this array and have them viewable at the same time under 1 index? For example at [0] projectName, projectManager, myMonths ie(string, string, integer). Any advice would be great, I am getting really confused with OOP. Thanks in advance!

1
  • Thanks for editing the code! :) Commented Apr 9, 2022 at 15:23

1 Answer 1

1

Yes, create a class containing your three properties:

class MyContainer {
    
    public MyContainer(int durationMonths, String projectName, String projectManager) {
        this.durationMonths = durationMonths;
        this.projectName = projectName;
        this.projetManager = projectManager;
    }

    public int durationMonths;
    public String projectName;
    public String projectManager;
}

Then create an array of this class with:

MyContainer[] myArray = new MyContainer[numberOfProjects];

Add items to the array like this:

myArray[0] = new MyContainer(3, "super project", "awesome manager");
Sign up to request clarification or add additional context in comments.

5 Comments

Syntax error. It should be: MyContainer[] myContainers = new MyContainer[];
Thanks so much for the answer! Sorry I'm really confused with this stuff. Do I need to get rid of my array called myMonths[ ]?
Updated the example code, I'm writing this on mobile so I messed up with the array name as Hovercraft pointed out. Aleo111 you should replace it with the array of the objects that will hold your values.
I'm just wondering if I'm declaring my array too many times. I've declared it in my main method as well as in my class file, am I doing this incorrectly? int myMonths[] = new int[amountOfProjects]; int index = 0; int num; while(choice !=6){ //while option 6 is not chosen keep showing the menu switch (choice){
in the message above you can see how I've declared it in my main method, just before I go into switch statements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.