So I am writing a program to read text from a file. Save from an array list to an array and then parse that array for ',' and use that to change words position in the 2d array i will write from the 1d array. So essentially String[]{"a,b,c","d,e,f"} ought to save to String[][]{{"a","b","c"},{"d","e","f"}}. but a null char is being added to the begenning ie: String[][]{{"nulla","nullb","nullc"},{"nulld","nulle","nullf"}}. This needs to be a 2d array so that i can place it into a JTable
Heres my code, I simply do not see what is happening here.

public String[][] getDat()
    {
        ArrayList<String> dataAL = new ArrayList<String>();

        try
        {
            Scanner file = new Scanner(new File ("dat.csv"));
            while (file.hasNext())
            {
                dataAL.add(file.nextLine());
            }
            file.close();
        }
        catch (FileNotFoundException fnfe)
        {
            System.out.println("File Not Found");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        String[] data = new String[dataAL.size()];
        dataAL.toArray(data);
        String[][]newData=new String[dataAL.size()][dataAL.size()];

        int k=0;
        for(int i=0; i<data.length;i++)
        {
            for(int j=0; j<data[i].length();j++)
            {
                if(data[i].charAt(j)==',')
                {
                    k++;
                }
                else
                {
                    newData[i][k]+=data[i].charAt(j);
                }
            }
            k=0;
        }

        return newData;
    }

Recommended Answers

All 7 Replies

You need to look more closely... there is no such thing as a null char in Java. What exactly are you seeing?

As i showed above: String[][] newData= {{"nulla","nullb","nullc"},{"nulld","nulle","nullf"}}.
IE: System.out.print(newData[0][0]);
Would output "nulla" where as i only want "a".

well, that array element contains "nulla" because that's what you put in there...

Can you explain what you are trying to achieve, recognising that there's no such thing as a null char?

OK, I just had a couple of idle minutes and read the whole code. There are no null chars (see above), but you do have references that are null, and you convert them to Strings.

Here's what seems to be happening:

You create a String[][] array, but you don't initialise it, so it's full of null references. Then you attempt
newData[i][k]+=data[i].charAt(j)
which is a short form of
newData[i][k] = newData[i][k] + data[i].charAt(j);
Java realises that the + can only be string concatenation at this point, so it converts both operands to String and concatenates them.
newData[i][k] converts to "null"
data[i].charAt(j) converts to a one-character long String (eg "a")
and concatenates them to give "nulla"

Solution: initialise your String[][] array properly.

That makes sense, I appreciate it. I just could not get my head around it, thanks a lot!

For anyone who might need this in the future the revised code here, line 29 & 35 being the additions:

 public String[][] getDat()
        {
            ArrayList<String> dataAL = new ArrayList<String>();

            try
            {
                Scanner file = new Scanner(new File ("dat.csv"));
                while (file.hasNext())
                {
                    dataAL.add(file.nextLine());
                }
                file.close();
            }
            catch (FileNotFoundException fnfe)
            {
                System.out.println("File Not Found");
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

            String[] data = new String[dataAL.size()];
            dataAL.toArray(data);
            String[][]newData=new String[dataAL.size()][dataAL.size()];
            int k=0;
            for(int i=0; i<data.length;i++)
            {
                newData[i][k]="";
                for(int j=0; j<data[i].length();j++)
                {
                    if(data[i].charAt(j)==',')
                    {
                        k++;
                        newData[i][k]="";
                    }
                    else
                    {
                        newData[i][k]+=data[i].charAt(j);
                    }
                }
                k=0;
            }
            return newData;
        }
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.