0

I would like to make a program where the user can input the number of variables and fill every variable with certain values. For example, the User inputs that he/she wants to make 10 arrays, then the User inputs that the first array should have 5 elements and the User fills that array with values, then the User wants the second array to have 4 elements and does the same and so on.

This is the code I was using, but it doesn't work:

public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the numbers of variables: ");
    int i = s.nextInt();

    for(int j = 0;j < i;j++){
        int[] var = new int[j];
        System.out.println("Enter the number of values: ");
        int p = s.nextInt();
        for(int q = 0;q < p;p++){
            int n = s.nextInt();
            var[q] = n;
        }
    }
}

And how could I compare these arrays that the user inputs?

2
  • 1
    you keep trashing var on every iteration, so effectively you only ever have one array. maybe you want an array of arrays... Commented Oct 14, 2016 at 15:17
  • Have you tested it? Commented Oct 14, 2016 at 15:26

3 Answers 3

1

The problem is that each time you are creating the array. try this:

Scanner s = new Scanner(System.in);
System.out.println("Enter the numbers of variables: ");
int i = s.nextInt();
int[][] var = new int[i][];
for(int j = 0;j < i;j++){

    System.out.println("Enter the number of values: ");
    int p = s.nextInt();
    var[j] = new int[p];
    for(int q = 0;q < p;p++){
        int n = s.nextInt();
        var[j][q] = n;
    }
}

Instead of creating a one dimensional array, you create a jagged array. Essentially, a 2d array is an array of arrays. that way the user inputs the number of arrays (i) and then continues to fill the arrays.

To check whether two collections have no commons values, you can use

Collections.disjoint();

For other operations, you can look here

Sign up to request clarification or add additional context in comments.

5 Comments

It should be called a jagged array instead of a 2D array.
And how could I compare these arrays?
Like I said, comparing them depends on their sizes, and is only possible if they are all the same size. In general, comparing arrays is messy.
I don't point at like comparing, I'm pointing at something like A union B union C or A intersection B
@Michael you can accept the answer, so others know it works when they find it.
0

This should work (with bidimensionnal array)

    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the numbers of variables: ");
        int i = s.nextInt();
        int[][] var = new int[i][];
        for(int j = 0;j < i;j++){
            System.out.println("Enter the number of values: ");
            int p = s.nextInt();
            var[j] = new int[p];
            for(int q = 0;q < p;q++){
                int n = s.nextInt();
                var[j][q] = n;
            }
        }
    }

You have to replace the incrementation in the second loop too ("q++" instead of "p++")

Comments

0

This should work and solve their first point

    Scanner s = new Scanner(System.in);
    System.out.println("Enter the numbers of variables: ");
    int i = s.nextInt();

    int[][] var = new int[i][];

    for(int j = 0;j < i;j++){
         System.out.println("Enter the number of values: ");
         int p = s.nextInt();
         while (p>0)
         {
            var[j] = new int[p];
            for(int q=0;q < p;q++){
               System.out.println("Value number : " +(q+1) + " For Array Number "+  (j+1));
               int n = s.nextInt();
               var[j][q] = n;
            }
             p-=1;
         }
    }

Comments

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.