0

I'm trying to create a multi-dimensional array in Java and I have it set up correctly however at the end it is saying '{' expected when there's already one there. This is the error line within the code

{
"Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59","50.05"
};

Any suggestions on a way to fix this problem?

Edit:

Before this line is the rest of the array and this coding:

import javax.swing.JOptionPane;

public class CMS_Program
{
   public CMS_Program()
      {
         String[][] names = new String[][]
            {
               { Array here

All { are closed off too at the end.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Josh
  • 1
  • 1
  • 2
    More context around this line? Seems like you might need to remove the `;` if this is part of a multi-dimensional array. – moinudin Jan 14 '11 at 14:33
  • Added. I had to cut the array out as the array is working fine and dandy it's just the ending when it's clearly is there. – Josh Jan 14 '11 at 14:37
  • 1
    Have you tried replacing the `;` with a `,`? The "inner" array defininitions in an MD array need to be separated by `,`, like any other array element. – thkala Jan 14 '11 at 14:39
  • 2
    A short but complete class demonstrating the problem would really help. – Jon Skeet Jan 14 '11 at 14:41
  • In the question title you say expected '}', in the body you say it was expecting a '{'. Which is it? A fuller context would also be appreciated. – Dunes Jan 14 '11 at 14:43
  • BTW: the 'new String[][]' is redundant and can be dropped. – Peter Lawrey Jan 14 '11 at 14:56

6 Answers6

7

Lot of context is still missing from your question. Anyway, the direct initialization of a String[][] ought basically to be done as follows:

String[][] names = new String[][] {
    { "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59", "50.05" },
    { "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59", "50.05" },
    { "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59", "50.05" },
    { "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59", "50.05" },
    { "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59", "50.05" }
};

However, you're better off using a List<Person> where the Person class look like this.

public class Person {
    private String name;
    private String id; // ??
    private Gender gender;
    private String city; // ???
    private Double time; // Or so?
    // ...
    // Add/generate c'tor/getter/setter/equals/hashcode and other boilerplate.
}

This way you can just end up with

List<Person> persons = new ArrayList<Person>();
persons.add(new Person("Gerald Field", "U18", Gender.MALE, "Bourges", 14.01, 26.59, 50.05));
// ...

Just work with real objects/entities and don't fiddle low-level with complex arrays. Your code will become more self-documented and better maintainable.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I think that it is not really a good practice to pass so many arguments in the constructor. You can avoid it with a builder class using fluent interface. See http://stackoverflow.com/questions/1632058/java-constructor-with-large-arguments-or-java-bean-getter-setter-approach – Bence Olah Jan 14 '11 at 15:00
  • 1
    @Bence: it was just a kickoff example to do the first step to do *it* the right way. – BalusC Jan 14 '11 at 15:11
2

Try this:

String[][] twoDimensional = {{"00", "01"}, {"10", "11"}};
Bence Olah
  • 644
  • 6
  • 10
0

It looks like you are doing this:

String[][] names = new String[][]
            {
                {
                  "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59","50.05"
                };

Note that there is a missing closing '}' If the closing brace is not missing then the semicolon needs to be after the second closing brace and not the first.

Jim
  • 22,354
  • 6
  • 52
  • 80
0

This should work out fine.

 String[][] names = new String[][]
       {
          {"ramalam", "wam wam"},
          {"ramalam", "wam wam"}
       };

Could it be that you had a semi-colon after the array?

user373455
  • 12,675
  • 4
  • 32
  • 46
0

This is valid:

String[][] names = new String[][]
                                {
                                   {
                                           "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59","50.05"
                                   }
                                };

I can't see how this differs from your source though...

John Pickup
  • 5,015
  • 2
  • 22
  • 15
0

{ and } are the beginning and end symbols of an array, and , is used to delimit the elements in the array.. If you create a multidimensional array (basically an array of array you need to use {..} for the array that is declared, as well as for any element within, because those are arrays too.

So, use something like this:

String[][] myMultiDimensionalArray = new String[][]
 {
   {
     "Gerald Field", "U18", "Male", "Bourges", "14.01", "26.59","50.05"
   },
   {
     "Name Lastname", "U18", "Male", "Bourges", "14.01", "26.59","50.05"
   }
 }

What the error is trying to say is that it sees only one dimension, and it was let to believe that there will be two.

SWeko
  • 30,434
  • 10
  • 71
  • 106