开发者

'}' expected error in Java however it's already there

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.


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.


Try this:

String[][] twoDimensional = {{"00", "01"}, {"10", "11"}};


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.


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?


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...


{ 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜