Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3282460
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:55:50+00:00 2026-05-17T19:55:50+00:00

I have a multidimensional array built from Strings that is initially created with the

  • 0

I have a multidimensional array built from Strings that is initially created with the size [50][50], this is too big and now the array is full of null values, I am currently trying to remove these said null values, I have managed to resize the array to [requiredSize][50] but cannot shrink it any further, could anyone help me with this? I have scoured the internet for such an answer but cannot find it.

Here is my complete code too (I realise there may be some very unclean parts in my code, I am yet to clean anything up)

import java.io.*;
import java.util.*;

public class FooBar
{
  public static String[][] loadCSV()
  {
    FileInputStream inStream;
    InputStreamReader inFile;
    BufferedReader br;
    String line;
    int lineNum, tokNum, ii, jj;
    String [][] CSV, TempArray, TempArray2;

    lineNum = tokNum = ii = jj  = 0;
    TempArray = new String[50][50];

    try
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter the file path of the CSV");
        String fileName = in.readLine();
        inStream = new FileInputStream(fileName);
        inFile = new InputStreamReader(inStream);
        br = new BufferedReader(inFile);
        StringTokenizer tok,tok2;


        lineNum = 0;
            line = br.readLine();
            tokNum = 0;
            tok = new StringTokenizer(line, ",");

            while( tok.hasMoreTokens())
            {
                TempArray[tokNum][0] = tok.nextToken();
                tokNum++;
            }
            tokNum = 0;
            lineNum++;

            while( line != null)
            {
                    line = br.readLine();
                    if (line != null)
                    {
                        tokNum = 0;
                        tok2 = new StringTokenizer(line, ",");

                        while(tok2.hasMoreTokens())
                        {
                            TempArray[tokNum][lineNum] = tok2.nextToken();
                            tokNum++;
                        }
                    }
                    lineNum++;
            }
    }    
    catch(IOException e)
    {
        System.out.println("Error file  may not be accessible, check the path and try again");
    }

    CSV = new String[tokNum][50];
    for (ii=0; ii<tokNum-1 ;ii++)
    {
        System.arraycopy(TempArray[ii],0,CSV[ii],0,TempArray[ii].length);
    }
    return CSV;
}    
public static void main (String args[])
{
    String [][] CSV;
    CSV = loadCSV();
    System.out.println(Arrays.deepToString(CSV));
}
}                

The CSV file looks as follows

Height,Weight,Age,TER,Salary
163.9,46.8,37,72.6,53010.68
191.3,91.4,32,92.2,66068.51
166.5,51.1,27,77.6,42724.34
156.3,55.7,21,81.1,50531.91

It can take any size obviously but this is just a sample file.

I just need to resize the array so that it will not contain any null values.

I also understand a list would be a better option here but it is not possible due to outside constraints. It can only be an multi dimensional array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:55:51+00:00Added an answer on May 17, 2026 at 7:55 pm

    I think you need 3 changes to your program

    • After your while loop lineNum will be 1 more than the number of lines in the file so instead of declaring CSV to String[tokNum][50] declare it as CSV = new String[tokNum][lineNum-1];
    • tokNum will be the number of fields in a row so your for loop condition should be ii<tokNum rather than ii<tokNum-1
    • The last parameter for your arraycopy should be lineNum-1

    i.e. the modified code to build your CSV array is:

    CSV = new String[tokNum][lineNum-1];
    for (ii=0; ii<tokNum ;ii++)
    {
        System.arraycopy(TempArray[ii],0,CSV[ii],0,lineNum-1);
    }
    

    and the output will then be:

    [[Height, 163.9, 191.3, 166.5, 156.3], [Weight, 46.8, 91.4, 51.1, 55.7], 
    [Age, 37, 32, 27, 21], [TER, 72.6, 92.2, 77.6, 81.1],
    [Salary, 53010.68, 66068.51, 42724.34, 50531.91]]
    

    Notice that you don’t really need to handle the first line of the file separately from the others but that is something you can cover as part of your cleanup.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.