0

I am fairly new to Java but am trying my best to learn. As a personal experiment I am writing a program that times the film developing process, fairly similar to the 'Massive Dev Chart' for Android and iPhone which I use quite a lot.

Following the code at: https://www.youtube.com/watch?v=QeaXXpxNtq0, I have the following code to read a CSV file and generate a 2 dimensional array:

static String xStrPath;
static String[][] myArray;

static void setUpMyCSVArray() {
    myArray = new String[8702][8];

    Scanner scanLn = null;
    int Rowc = 0;
    int Row = 0;
    int Colc = 0;
    int Col = 0;
    String InputLine = "";
    double xnum = 0;
    String xfileLocation;

    xfileLocation = "database.csv";

    System.out.println("\n****** Setup Array ******");

    try {
        scanLn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));

        while (scanLn.hasNextLine()) {
            InputLine = scanLn.nextLine();
            String[] InArray = InputLine.split(",");
            for (int x = 0; x < InArray.length; x++) {
                myArray [Rowc] [x] = InArray[x];

            }
            Rowc++;
        }
    } catch (Exception e) {
        System.out.println(e);
    }

    System.out.println(Arrays.toString(myArray[0]));
}

The CSV file looks like this:

Film Name        Developer    Dilution    ISO/ASA  35mm  120  Sheet  Temperature
Adox CHM 125,    510-Pyro,    1+100,      125,     7,    ,    ,      21C
Adox CHM 125,    510-Pyro,    1+500,      125,     50,   ,    ,      21C
Adox CHM 125,    777,         stock,      100,     12,   ,    ,      24C
Adox CHM 125,    A03,         stock,      125,     6.5,  ,    ,      20C
Adox CHM 125,    ACU-1,       1+10,       100,     9,    ,    ,      21C
Adox CHM 125,    Acufine,     stock,      125,     4,    ,    ,      20C
Adox CHM 125,    Acufine,     stock,      200,     6,    ,    ,      20C
Adox CHM 125,    Acufine,     stock,      320,     5,    ,    ,      21C

When I run:

System.out.println(Arrays.toString(myArray[0]));

The output is:

Adox CHM 125,    510-Pyro,    1+100,      125,     7,    ,    ,      21C

But I am unable to separate the data on that line, i.e. I want to run

System.out.println(Arrays.toString(myArray[0][0]));

To output:

Adox CHM 125

After many attempts I am a little stuck as to what is going wrong with my code, any help or advice would be much appreciated.

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
TheVDM
  • 29
  • 6
  • I think you are not correctly reading the splited String. Take a look at this: http://stackoverflow.com/questions/5360628/get-and-parse-csv-file-in-android – BBacon Jul 25 '14 at 21:10
  • What does it print when you run `System.out.println(Arrays.toString(myArray[0][0]));` ? ANyway i think you can just ommit the `Arrays.toString` part. Just print `myArray[0][0]` – gkrls Jul 25 '14 at 21:18

2 Answers2

1

You use Arrays.toString if you want to print out a whole 1D array, because it convertes an array to a string. If you want to print out just one element of the array (like myArray[0][0] as you indicated), you would simply do this:

System.out.println(myArray[0][0]);

No need for the Arrays.toString there, because the elements of your jagged array are already Strings.

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
  • Thank you, that did exactly what I wanted, looking at the answer it may be obvious that I am a Java rookie. I will accept the answer in a few minutes when stack overflow allows me to. Thank you – TheVDM Jul 25 '14 at 21:12
  • The answer may be obvious to me, but it wasn't to you, and there's no need to feel bad about it. I'm just glad I could help. :) – Jashaszun Jul 25 '14 at 21:14
  • Thank you again, I suppose we all have to start somewhere :D – TheVDM Jul 25 '14 at 21:19
1

myArray is an array, actually an array of arrays. myArray[0] is therefore an array. You can use Arrays.toString() on an array.

But myArray[0][0] is not an array, but rather an individual element. Therefore Arrays.toString() can't be used on it. It won't compile.

Since each element myArray[i][j] is a String, you don't need to do anything else to the element--just use it.

System.out.println(myArray[0][0]);
ajb
  • 31,309
  • 3
  • 58
  • 84