-2

I'm not that experienced with coding but I know a thing or two and when trying to do something I encountered a problem.

The code itself:

public class Main {

    public static void main(String[] args) 
    {

        folder test = new folder("test", [5] , [5] );
        int i = test.containedFolders.length;
        System.out.println(i);
    }
}

The constructor:

public class folder
{

    private String name;
    private String[] containedFolders;
    private String[] containedFiles;

    folder(String Name, String[] ContainedFolders, String[] ContainedFiles)
    {
        name = Name;
        containedFolders = ContainedFolders;
        ContainedFiles = ContainedFiles;
    }
}

As seen in the title I get this error message- "Syntax error on token ",", Expression expected after this token" on both of the "," in ("test", [5] , [5] ).

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
  • 3
    `[5]` is not a valid array of strings – twain249 Sep 15 '17 at 22:50
  • 1
    1) Always, **always**, ALWAYS stick to Java naming conventions - especially if you claim to know "_a thing or two_" - classes are in `PascalCase`, variables are `camelCase`. 2) How exactly is `[5]` a) valid syntax or b) a `String[]`? – Boris the Spider Sep 15 '17 at 22:52

1 Answers1

0

Your folder class constructor

folder(String Name, String[] ContainedFolders, String[] ContainedFiles)

is expecting a string, then 2 string arrays.

so you want to write your constructor as:

folder test = new folder("test", new String[] {"5"}, new String[] {"5"} );

The difference between this one and yours is that Java uses curly braces {} to denote an array literal. And you also need to put quotes around the 5's to denote them as strings.

  • This doesnt really work for me (could be me typing it somehow wrong but i think i managed to fix that by typing: folder test = new folder("test", new String[5] , new String[5] ); Do you think it really solves it or just mess it in some different way? btw thx for answering anyways! – Just a Salad Knight Sep 15 '17 at 22:56
  • `new String[] {"5"}` is an array of one string which is `"5"`. Yours is an uninitialized array of 5 strings which are `null`. – kichik Sep 15 '17 at 23:01
  • kichik is right, new String[5] just creates an empty array with 5 slots, new String[] {"5"} uses array literals to define a new array of strings with "5" in it. – Marco Simone Sep 15 '17 at 23:19