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.

2
  • I think you are not correctly reading the splited String. Take a look at this: stackoverflow.com/questions/5360628/… Commented Jul 25, 2014 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] Commented Jul 25, 2014 at 21:18

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
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. :)
Thank you again, I suppose we all have to start somewhere :D
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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.