0
for(i=0; i<=2; i++){
    if(i=0){
        System.Out.println("Input x: ");
        int x=input.nextInt();
        if(x==1){
            char[] a={'A','B','C'};
        }
        else if(x=2){
            char[] a={'D','E','F'};
        }
        else{
           char[] a={'G','H','I'};
        }
    } 

Values of a[] will change 3 times because of for loop & the values decided by input x. My question here is, How can I store values of a[] in every loop to another variable and make another multidimensional array with those values????? please anyone help me for this. Thanks in advance.

2
  • 3
    Watch out for the first and third if statements: you're using the assignment operator, not equality! Commented Aug 25, 2016 at 13:35
  • What problems doyou have? As far as I see your code will not compile. Is it a problem you want to solve? Commented Aug 25, 2016 at 13:43

3 Answers 3

1

It is hard to be sure what you are actually looking for, but this might give you some ideas. At least, the syntax should be correct:

char[][] array = new char[3][];
for (int i = 0; i < array.length; i++) {
    System.out.println("Input x: ");
    int x = input.nextInt();
    if (x == 1) {
        array[i] = new char[] {'A', 'B', 'C'};
    } else if (x == 2) {
        array[i] = new char[] {'D', 'E', 'F'};
    } else {
        array[i] = new char[] {'G', 'H', 'I'};
    }
}

Things to note:

  1. Case is significant. It is System.out not System.Out.
  2. Use = for assignment and == to test for equality for primitive types. (But generally NOT other types!)
  3. Correct indentation and consistent use of whitespace is important for readability. Try to conform to style guidelines.
Sign up to request clarification or add additional context in comments.

1 Comment

You "Things to note" have problem with case. What an irony :)
0

Some code to get you going:

char[][] matrix = new char[2][4];
for (int i=0; i < 2; i++) {
  // now create an array for the columns
  matrix[i]= new char[4];
  // now you could do
  for (int j=0; j < 4; j++) {
    matrix[i][j] = ...
  }
  // or
  char[] row = { '1', '2', '3', '4' };
  matrix[i] = row;
}

The idea is that you first say how many rows and columns you have. Then you iterate the first dimension, and you can set the values for the second dimension during each iteration.

4 Comments

@StephenC Why not?
The array initializer was correct, the assignment was not.
Thanks, again. Updated the example part.
@user1803551 - I was referring to the original version of GhostCat's code. The "why not" is that the code was syntactically invalid. The point is that that kind of initializer can only be use in a declaration initializion or a new ... not in a plain assignment.
0

I would rather solve your problem based on a requirement than a rather non-suggestive code, but here goes:

final int total=2;
char[][] a=new char[total][];
for (int i=0;i<total;i++){
     System.Out.println("Input x: ");
     int x=input.nextInt();
     switch(x){
         case 1:
             a[i]=new char[]{'A','B','C'};
         break;
         //Other cases...
     }
}

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.