0

i am trying to take input from the using in a 2d char array where the output should be like :

110

1_0

11_

_11

0__

this can have as many combinations as 2^n where n is also user input. how can i create this output?

public static void main (String args[])

{ Scanner sc = new Scanner(System.in); 
  System.out.println("Enter Value i:  ");
      int i = sc.nextInt();
  int j =(int) Math.pow(2,i);
  char[][]array = new char[i][j];
      for (int k=0;k<i;k++)
    for (int s=0;s<j;s++)
    { array[k][s]= ?;  //i am stuck here

        }
5
  • 1
    Why the output should be like that? Commented Nov 17, 2013 at 20:41
  • I didn't understand why there is _ in output! Commented Nov 17, 2013 at 20:44
  • that is a don't care value in the truth table. Commented Nov 17, 2013 at 20:53
  • You say that you going to get the input. Why should we care about the output? You do not need a scanner for such simple thing. How reading data for 2d array is different from reading data for 1-d array? Commented Nov 17, 2013 at 20:59
  • i need that output for my other piece of code for that i need the track of rows and columns. Commented Nov 17, 2013 at 21:06

2 Answers 2

1
Scanner scan=new Scanner(System.in);

char inputArray[][] = new char[rows][cols];

for (int i = 0; i < rows; i++)
{ 
    for (int j = 0; j < cols; j++)
    { 
        inputArray[i][j] = scan.next().charAt(0);
    } 
} 
Sign up to request clarification or add additional context in comments.

1 Comment

This would be more useful with some text around the code, and better indentation
0
Scanner in = new Scanner(System.in);
int rows = scan.nextInt();
int cols = scan.nextInt();

char arr[][] = new char[rows][cols]; // 2D char array
        for (int i = 0; i < rows; i++) {
            String data = "";
            if (in.hasNext()) { // input from user 
                data = in.next();
            } else {
                break;
            }
            for (int j = 0; j < cols; j++)
                arr[i][j] = data.charAt(j); 
        }
// to get a 2D char array
System.out.println(Arrays.deepToString(arr));

The input will be of the form -

4 // no. of rows
5 // no. of columns
10100
10111
11111
10010

to get a 2D char array as-

[[1, 0, 1, 0, 0], [1, 0, 1, 1, 1], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0]]

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.