Home arrow static arrow Java Programming [Archive] - Wierd Array Error...
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Wierd Array Error...
This topic has 4 replies on 1 page.

Posts:112
Registered: 10/8/03
Wierd Array Error...  
Jun 25, 2004 10:35 AM



 
Hi,

I am trying to create an array... any type of array in any class in my program, but I always get an error... no matter where or what size I set it. Right now I am trying to init the array in a method and use it there. Here is the code I am using
String[] plotLine = new String[20];plotLine[ 0] = "Eye of the Dragon";plotLine[ 1] = "__________________";plotLine[ 2] = "";plotLine[ 3] = "In the far north of SaelRoth";plotLine[ 4] = "An ancient evil has been awoken..";plotLine[ 5] = "";plotLine[ 6] = "All of the dark things in this world rally to it's call";plotLine[ 7] = "";plotLine[ 8] = "Everything is lay still, silent, and unknowing, as a black cloud decends into this world,";plotLine[ 9] = "which has been softened by too many years of peace..";plotLine[10] = "";plotLine[11] = "In the world starved of powerful mortal heroes,";plotLine[12] = "only one person can stop him......";plotLine[13] = "";plotLine[14] = "";plotLine[15] = "";plotLine[16] = "You are a new recruit into the Red Gaurd of Manetherin";plotLine[17] = "you are just out of training, and they have given you a day to rest,";plotLine[18] = "and were told then to go straight to the Calptain-General Gherrik afterwords";plotLine[19] = "to recieve your duties";

My error is as follows
RPGMain.java:677: ']' expected
plotLine[ 0] = "Eye of the Dragon";
^
RPGMain.java:677: <identifier> expected
plotLine[ 0] = "Eye of the Dragon";
^
RPGMain.java:678: ']' expected
plotLine[ 1] = "__________________";
^
RPGMain.java:678: <identifier> expected
plotLine[ 1] = "__________________";
^
RPGMain.java:679: ']' expected
plotLine[ 2] = "";
^
RPGMain.java:679: <identifier> expected
plotLine[ 2] = "";
^
RPGMain.java:680: ']' expected
plotLine[ 3] = "In the far north of SaelRoth";
^
RPGMain.java:680: <identifier> expected
plotLine[ 3] = "In the far north of SaelRoth";
^
RPGMain.java:681: ']' expected
plotLine[ 4] = "An ancient evil has been awoken..";
^
RPGMain.java:681: <identifier> expected
plotLine[ 4] = "An ancient evil has been awoken..";
^
RPGMain.java:682: ']' expected
plotLine[ 5] = "";
^
RPGMain.java:682: <identifier> expected
plotLine[ 5] = "";
^
RPGMain.java:683: ']' expected
plotLine[ 6] = "All of the dark things in this world rally to it's ca
ll";
^
RPGMain.java:683: <identifier> expected
plotLine[ 6] = "All of the dark things in this world rally to it's ca
ll";

^
RPGMain.java:684: ']' expected
plotLine[ 7] = "";
^
RPGMain.java:684: <identifier> expected
plotLine[ 7] = "";
^
RPGMain.java:685: ']' expected
plotLine[ 8] = "Everything is lay still, silent, and unknowing, as a
black cloud decends into this world,";
^
RPGMain.java:685: <identifier> expected
plotLine[ 8] = "Everything is lay still, silent, and unknowing, as a
black cloud decends into this world,";

^
RPGMain.java:686: ']' expected
plotLine[ 9] = "which has been softened by too many years of peace.."
;
^
RPGMain.java:686: <identifier> expected
plotLine[ 9] = "which has been softened by too many years of peace.."
;

^
RPGMain.java:687: ']' expected
plotLine[10] = "";
^
RPGMain.java:687: <identifier> expected
plotLine[10] = "";
^
RPGMain.java:688: ']' expected
plotLine[11] = "In the world starved of powerful mortal heroes,";
^
RPGMain.java:688: <identifier> expected
plotLine[11] = "In the world starved of powerful mortal heroes,";
^
RPGMain.java:689: ']' expected
plotLine[12] = "only one person can stop him......";
^
RPGMain.java:689: <identifier> expected
plotLine[12] = "only one person can stop him......";
^
RPGMain.java:690: ']' expected
plotLine[13] = "";
^
RPGMain.java:690: <identifier> expected
plotLine[13] = "";
^
RPGMain.java:691: ']' expected
plotLine[14] = "";
^
RPGMain.java:691: <identifier> expected
plotLine[14] = "";
^
RPGMain.java:692: ']' expected
plotLine[15] = "";
^
RPGMain.java:692: <identifier> expected
plotLine[15] = "";
^
RPGMain.java:693: ']' expected
plotLine[16] = "You are a new recruit into the Red Gaurd of Manetheri
n";
^
RPGMain.java:693: <identifier> expected
plotLine[16] = "You are a new recruit into the Red Gaurd of Manetheri
n";

^
RPGMain.java:694: ']' expected
plotLine[17] = "you are just out of training, and they have given you
a day to rest,";
^
RPGMain.java:694: <identifier> expected
plotLine[17] = "you are just out of training, and they have given you
a day to rest,";

^
RPGMain.java:695: ']' expected
plotLine[18] = "and were told then to go straight to the Calptain-Gen
eral Gherrik afterwords";
^
RPGMain.java:695: <identifier> expected
plotLine[18] = "and were told then to go straight to the Calptain-Gen
eral Gherrik afterwords";

^
RPGMain.java:696: ']' expected
plotLine[19] = "to recieve your duties";
^
RPGMain.java:696: <identifier> expected
plotLine[19] = "to recieve your duties";
^
40 errors


Any Ideas on how to fix this?
 

Posts:31,095
Registered: 4/30/99
Re: Wierd Array Error...  
Jun 25, 2004 10:40 AM (reply 1 of 4)



 
Somewhere before that code fragment, probably, there is a missing ']" character. The compiler keeps looking for it and ignoring the stuff you have there (which BTW contains at least seven spelling errors).
 

Posts:10,972
Registered: 10/23/03
Re: Wierd Array Error...  
Jun 25, 2004 10:58 AM (reply 2 of 4)



 
By not posting a complete minimal program you've made the error trickier to see that it should be.
In fact, it's an obvious error and not at all weird: you are trying to write assignment statements
directly inside a class definition.

In this demo I recreate your error then list several ways to do it right.
class Example {    String[] plotLine = new String[20];    plotLine[0] = "Eye of the Dragon";} class Example2 {    static String[] plotLine1 = new String[20];     static {        plotLine1[0] = "Eye of the Dragon";    }     static String[] plotLine2 = {        "Eye of the Dragon",        "etc.."    };     String[] plotLine3 = new String[20];     public Example2() {        plotLine3[ 0] = "Eye of the Dragon";    }     String[] plotLine4 = {             "Eye of the Dragon",             "etc.."    }public void method3() {        String[] plotLine5 = new String[20];        plotLine5[ 0] = "Eye of the Dragon";         String[] plotLine6 = {                "Eye of the Dragon",                "etc.."        };    }}
 

Posts:3,055
Registered: 18/06/98
Re: Wierd Array Error...  
Jun 25, 2004 11:01 AM (reply 3 of 4)



 
Strange - I've put your code inside a method and it compiles OK.

Probably you are trying to put the code outside a method; In this case it will generate the errors you've seen.

An alternative way of making your program work:
public class Game {    String[] plotLine = {        "Eye of the Dragon",        "______",        "In the far north of SaelRoth",  .... and so on ....    };}


You do not need to count the strings, or numbering them.
 

Posts:112
Registered: 10/8/03
Re: Wierd Array Error...  
Jun 25, 2004 11:05 AM (reply 4 of 4)



 
thanks... I still don't know why it did that, but I took edsonw suggestion and worked it to fixed thanks.
 
This topic has 4 replies on 1 page.