Search code examples
javaarrayschars

How to convert Strings to 2d char Array


I have a text file like this

A B C D E F
AABG EF 123
AD AD POLFE
APF PLF ADS

I'm trying to put each characters including blank spaces into a 2D array;

I tried this

data = new ASCIIDataFile();
int rowLen =0;
String s = "";
while(data.isEOF()!=true){
    iArray[rowLen] = data.readLine().toCharArray();
    rowLen++;
    }

but this will give me a null pointer. How can I put them in a 2d char array?


Solution

  •     Try This demo:
    
       import java.io.*;
       import java.util.*;
        class d
        {
        public static void main(String args[])
       {
            try
                {
             FileReader fr = new FileReader("String.txt");
             BufferedReader br = new BufferedReader(fr);
             String s="";
             char [] r2;
             int i=0,j=0;
             char [][]c=new char[5][12];
             while((s = br.readLine()) != null) 
             {
             r2=s.toCharArray();
    
             for(j=0;j<r2.length;j++)
             {
              c[i][j]=r2[j];
              System.out.print(""+c[i][j]);
              System.out.print(",");
            }
            i++;
            System.out.print("\n");
                }
            }
           catch(Exception e)
            {
             e.printStackTrace();
             }
           }
        }
    

    enter image description here