I am trying to read a CSV file having no. of rows=row and no. of columns=col by the following method:

public static String[][] FileIntoArray(int row, int col, String FileName){
        int Filerow=0;
        int Filecol=0;
        String line;
        String[][] FileArray= new String[row][col];

        File file = new File(FileName);


            try{
                BufferedReader bf  = new BufferedReader(new FileReader(file));
                try {
                    while((line = bf.readLine()) != null){

                    StringTokenizer st = new StringTokenizer(line,",");
                    while (st.hasMoreTokens()){
                            FileArray[Filerow][Filecol]= st.nextToken();
                            Filecol++;                          
                            }


                    Filecol = 0;
                    Filerow++;
                    }

                }catch (IOException e){
                    }
            }catch(FileNotFoundException f){
                }
    return FileArray;

    } 

Now suppose, the code reads the file
1,2,3,4
,,4,5

As you could see, there are some entries absent on 2nd row. So what will it read then if the entries are not present?

Dani AI

Generated

used StringTokenizer(line, ",") in the posted code, so the behavior is not the same as returning empty strings for missing fields. StringTokenizer treats consecutive delimiters as a single separator, so a line like

1,2,3,4
,,4,5

will produce tokens ["1","2","3","4"] for the first row and only ["4","5"] for the second. Because the array was created with new String[row][col], any cells that are never assigned remain null (Java object arrays default to null), and the tokens from the second row will be placed starting at column 0 (data shifts left). That explains why missing entries disappear rather than becoming "", and it can also lead to misalignment or ArrayIndexOutOfBoundsExceptions if token counts differ from col.

A straightforward fix is to split lines while preserving empty fields and then copy tokens into the fixed-width row with bounds checks. For example:

String[] tokens = line.split(",", -1); // keep empty tokens
for (int c = 0; c < col; c++) {
    FileArray[rowIndex][c] = (c < tokens.length) ? tokens[c] : "";
}

Notes and best practices:

  • StringTokenizer is legacy; prefer String.split(..., -1) for simple CSVs or a proper CSV library (OpenCSV / Apache Commons CSV) when quotes, escaped commas, or newlines inside fields may occur.
  • Initialize rows to "" if downstream code expects empty strings instead of nulls (or explicitly test for null).
  • Add bounds checks for rowIndex/col, and use try-with-resources to ensure the reader is closed and exceptions are not silently swallowed.
  • Trim or validate numeric conversions after parsing to avoid surprises from whitespace or malformed fields.

This addresses the mismatch between 's remark (empty strings can be expected when splitting) and the actual effect of the posted implementation: with StringTokenizer the empty entries are skipped and the array cells remain null.

You should just get a zero-length String "" for the missing entries.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.