amoosh
amoosh

Reputation: 43

Char 2 dimensional

 public static void main(String[] args) {
      Scanner s= new Scanner(System.in);
      String m ;
      char cur;
      String k;
      System.out.print("enetr the text ");
      m=s.next();
      System.out.print("enetr the key: ");
      k=s.next();
      char d[][]= new char [m.length()][k.length()];

      for(int i=0;i<1;i++){
        for(int j=0;j<k.length();j++){
           cur=k.charAt(j);
           System.out.print(""+cur);

           d[0][j]=cur;
           System.out.print(d);

        }
      }

I would like to ask for help correcting this segment of code.

The idea of what this code needs to accomplish: I want to copy a sentence in a 2 dimensional array, so that the size of the matrix is ​the same length as the sentence; for example, if k is "jon", I expect to have

D[0][0]=j   D[0][1]=o   D[0][2]=n

Upvotes: 0

Views: 69

Answers (1)

Pradeep Pati
Pradeep Pati

Reputation: 5919

change the i loop to this

for(int i=0;i<m.length();i++)

change the d[0][j]=cur; line to d[i][j]=cur;

Upvotes: 1

Related Questions