help with use of arrays with arrays [closed]

0

I am learning java I am doing a program where I ask for 5 birthdays with name, month and day, and sort them according to the number of the year and numerically, at the moment I only have the days month and names in 3 different arrays and 3 cycles that run the arrays but I stagnate because when I run the program it goes well for all the arrays of names and days, when it comes to months ask for a month and then it goes on and on and asks for the 5 months and I want to have it ask for a name, day and month and so on until I complete the 5, this is my code.

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

public class birthday {
    public static void main(String args[]){ 
    Scanner input  = new Scanner(System.in);
    String[] words = new String[5];
    int[] month = new int [5];
    int [] num = new int[5];
    for (int i = 0; i < words.length; i++) {

        System.out.println("Birthday name?");
        words[i] = input.next();
        System.out.println(Arrays.toString(words));
        for(int a = 0; a<num.length; a++) {
            System.out.println("day ");
            num[a] = input.nextInt();
            System.out.println(Arrays.toString(num));
            for(int x = 0; x < month.length; a++) {
                System.out.println("number of month? ");
                month[x] = input.nextInt();
                System.out.println(Arrays.toString(month));
    }
   }
  }
 }  
}

this is the the program since I ran it

Birthday name?
pepe
[pepe, null, null, null, null]
day 
14
[14, 0, 0, 0, 0]
number of month? 
8
[8, 0, 0, 0, 0]
number of month? 
10
[8, 10, 0, 0, 0]
number of month? 
11
[8, 10, 11, 0, 0]
number of month? 

At the end only runs the last array that would be the month someone could help me as I can make one walk one by one (name, day, month, name, day month) so until the 5 thanks.

    
asked by Microplo 12.12.2017 в 19:50
source

1 answer

0

I comment, in the third cycle, you are increasing the variable a ++ not the x, therefore you will be in an infinite cycle, because x will always be less than the length of the array.

The second error is that you have 3 cycles, in doing so, your program will complete each cycle from the inside out, remaining as follows: In the first iteration, request first name, request first day, and request 5 months (long array), second iteration, request second name, request second day, and 5 months again, and so on.

My recommendation is this:

public class birthday {
    public static void main(String args[])
    { 
        int valor = 5;    
        Scanner input  = new Scanner(System.in);
        String[] words = new String[valor];
        int[] month = new int [valor];
        int [] num = new int[valor];
        for (int x = 0; x < valor; x++) {

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

            System.out.println("day ");
            num[x] = input.nextInt();
            System.out.println(Arrays.toString(num));

            System.out.println("number of month? ");
            month[x] = input.nextInt();
            System.out.println(Arrays.toString(month));

        }
    }  
}

In this way, with a single cycle, you make the request for all the information you need in each iteration. I hope it serves you.

    
answered by 12.12.2017 / 21:01
source