how to put in an array the result of the use of information of two arrays?

1

Hi I'm learning java, I'm doing a program where I talk about the birthdays of 5 people on days of the year (I only take the 365 days I do not take into account leap years) but I'm stuck I have the days and months in two arrays after I use those two pieces of information that the user previously put in to take out the day of the year but I want to store them in a new array since I will use it later to use a sort with that array, this is my code

import java.util.Arrays; import java.util.Scanner;

public class birthday {     private static Scanner input;

public static void main(String args[]){ 
    int[]  num2 = {31,59,90,120,151,181,212,243,273,304,334,365};

    int value = 2;    
    input = new Scanner(System.in);
    String[] words = new String[value];
    int[] month = new int [value];
    int [] num = new int[value];
    for (int x = 0; x < value; x++) {

        System.out.println("Enter a name");
        words[x] = input.next();
        System.out.println(Arrays.toString(words));

        System.out.println("Birth month?(1 to 12) ");
        month[x] = input.nextInt();
        System.out.println(Arrays.toString(month));

        System.out.println("day of the month(1 to 31) ");
        num[x] = input.nextInt();
        System.out.println(Arrays.toString(num));       

        int month1 = month[x];
        int day = num[x];

        int sumDays;
        if (month1==1) {

            sumDays = day;
        }else {

            sumDays = num2[month1-1-1] + day;
        }

        System.out.println("the day of the birthday is " + sumDays);
        {


        }


        }

}   
}

As you see, ask for name month and day, then use month and day to get the day of the year and at the end give an output

Enter a name
juan 
[juan, null]
Birth month?(1 to 12) 
8
[8, 0]
day of the month(1 to 31) 
14
[14, 0]
dia de cumple es  226

and at the end I want to put that 226 in an array jutno with the remaining 4, thanks for your help

    
asked by Microplo 09.01.2018 в 18:59
source

2 answers

2

You already have it resolved in your code.

Notice that in each iteration you are filling a vector with the value you ask for on the keyboard. With which, the only thing you have to do is save that value, as you do with the rest.

...
int[] DiasEnElAño = new int [value];
...
if (month1==1) {
   DiasEnElAño[x] = day;
}else {
   DiasEnElAño[x] = num2[month1-1-1] + day;
}
System.out.println("the day of the birthday is " + DiasEnElAño[x]);

It would clarify that this solution is useful, and as a first step it is fine. But it is not recommended, at this point I would think about building a matrix or a vector but containing items of a class. (I know it's an exercise, but I think that the complexity is extreme based on what is learned at this point).

    
answered by 09.01.2018 / 19:22
source
0

The best solution is to create a person object and in saving that data, you must create a new class of java with the following code:

public class persona {

int day;
int month;
int dayOfYear;

public persona(int day, int month, int dayOfYear) {
    this.day = day;
    this.month = month;
    this.dayOfYear = dayOfYear;
}

public int getDay() {
    return day;
}

public void setDay(int day) {
    this.day = day;
}

public int getMonth() {
    return month;
}

public void setMonth(int month) {
    this.month = month;
}

public int getDayOfYear() {
    return dayOfYear;
}

public void setDayOfYear(int dayOfYear) {
    this.dayOfYear = dayOfYear;
} }

When creating it you can already use it in a List and add the elements you need or that you "ask" in its constructor (int day, int month, int dayOfYear).

Now in your main class you create a list and it is initialized with an arraylist ()

List list = new ArrayList<persona>();

Now you should only add the elements you already received to that ArrayList

list.add(new persona(1,2,3));

I hope you solve what you need, I see that you are learning java, but if you really want to dedicate yourself to programming for android I recommend you to learn Kotlin, personally I like it more and it is much more "friendly" when writing and reading code, all The previous code in Kotlin can be summarized in three lines.

KOTLIN

create person object

data class persona (val day:Int, val month: Int, val dayOfYear: Int)

Access and add elements

var list = ArrayList<persona>()
list.add(persona(1,2,3))

Greetings and I leave here a link to the official documentation of Kotlin

    
answered by 09.01.2018 в 19:34