adding variables from a Java array

0

I have an arrangement with 12 elements, each one is a month of the year and they are assigned a number of days respectively

int Enero=31;
int Febrero=28;

int diciembre=31;

int cantidades[]=   {Enero,Febrero,Marzo,Abril,Mayo,Junio,Julio,Agosto,Septiembre,Octubre,Noviembre};

I also have a couple of attributes called Dia and Mes with which I have to count down to know how many days are left until the first of January of the following year. say enter two numbers for example 1 (Month) 2 (Day). So far I have the following but it does not work because the index goes beyond the limit but I do not know how to solve it in a way that works (ArrayIndexOutOfBoundsException: -1 at Dates.Regular account)

I need to count the days of each month along with the days of the current month

public int cuentaRegresiva(){
 int diasTotales;
 int diasTranscurridos=0;
 int diasFaltantes;
 int iterador=Mes;
 int contador=2;

while (contador<13){
 diasTranscurridos+=cantidades[iterador-contador];
 contador+=1 }
 diasTranscurridos=diasTranscurridos+Dia;'
}
    
asked by zedsdeath 04.09.2017 в 08:02
source

3 answers

1

First, I do not know if it will be a mistake to copy it here, but in the array of quantities it would be missing December:

int cantidades[]={Enero,Febrero,Marzo,Abril,Mayo,Junio,Julio,Agosto,Septiembre,Octubre,Noviembre, Diciembre};

Regarding the error, it is referred to that you are trying to access non-existent positions in the array (which would go from 0 to 11 in this case), that is, in the while loop you are trying to access negative positions and that, for example, if you wanted to try to take out the days of March, the counter variable would continue going up to 12, trying to access positions as quantities [-5]. Something more correct would be to start iterator equal to Month-1 (for example, March represents position 2) and add the days of months that are greater than or equal to 0 that is January (accessing only the variable iterator and eliminating the variable counter), that is:

public int cuentaRegresiva(){
        int diasTotales;
        int diasTranscurridos=0;
        int diasFaltantes;
        int iterador=Mes-1;
        while (iterador>0){
             diasTranscurridos+=cantidades[iterador];
             iterador-=1; }
        diasTranscurridos=diasTranscurridos+Dia;
    }
    
answered by 04.09.2017 в 08:22
0

You can use a variable of sum that stores first the days that takes the month in particular:

diasTotales = cantidades[mes - 1] - dia;

And then add to that variable all the days missing in your arrangement:

for (int i = mes; i < cantidades.length; i++) {
    diasTotales += cantidades[i];
}

I have done a small class that exemplifies it:

public class SumArray
{
    private int[] cantidades = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    private Scanner input = new Scanner(System.in);

    public void contarDias()
    {
        int dia;
        int mes;
        do
        {
            dia = input.nextInt();
            mes = input.nextInt();
            System.out.println(cuentaRegresiva(dia, mes));
        } while (dia > 0 && mes > 0);
    }

    private int cuentaRegresiva(int dia, int mes)
    {
        int diasTotales = cantidades[mes - 1] - dia;
        for (int i = mes; i < cantidades.length; i++)
        {
            diasTotales += cantidades[i];
        }
        return diasTotales;
    }


    public static void main(String[] args)
    {
        new SumArray().contarDias();
    }
}
    
answered by 04.09.2017 в 20:36
0

First you have to add the complete months that you lack until the following year and then the days of the month you are in, besides, as you said @ Galante96 you lack December in your array.

To add the days of the full months you have to do:

for(int i=11; i>=mes; i--){
    diasTotales = diasTotales + meses[i];
}

Now you only have to add the days that you have left of the month in which you are:

diasTotales = diasTotales + meses[mes] - dias;

Between all you would be:

public int cuentaRegresiva(int dia, int mes){
    int diasTotales = 0;

    for(int i=11; i>=mes; i++){
        diasTotales = diasTotales + cantidades[i];  //Sumamos los días de los meses completos hasta fin de año
    }

    diasTotales = diasTotales + cantidades[mes-1] - dia;  //Sumamos los días del mes introducidpo que nos faltan hasta fin de mes

    return diasTotales;
}
    
answered by 04.09.2017 в 08:33