• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Illegal initializer for java.lang.String

 
Sudheer Kolanu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing an example given in Head First Java but it is giving me 7 errors, could not find out the reason for it. The below is the code and compiled errors, please advise.



Errors

C:\Library\Headache\Pro>javac Phrase.java
Phrase.java:5: illegal initializer for java.lang.String
String wordListOne={"Dave","Nick","Paul","Mark","John","Bill","Gil"};
^
Phrase.java:7: illegal initializer for java.lang.String
String wordListTwo={"is","was","isNot","wasNot"};
^
Phrase.java:9: illegal initializer for java.lang.String
String wordListThree={"a good boy","a good student","a good worker","a good thin
ker", "a good teacher"};
^
Phrase.java:20: array required, but java.lang.String found
String finalPhrase = wordListOne[rand1]+" "+wordListTwo[rand2]+" "+wordListTwo[r
and3];
^
Phrase.java:20: array required, but java.lang.String found
String finalPhrase = wordListOne[rand1]+" "+wordListTwo[rand2]+" "+wordListTwo[r
and3];
^
Phrase.java:20: array required, but java.lang.String found
String finalPhrase = wordListOne[rand1]+" "+wordListTwo[rand2]+" "+wordListTwo[r
and3];
^
Phrase.java:22: cannot find symbol
symbol : variable finalPharse
location: class Phrase
System.out.println("This is the Phrase "+finalPharse);
^
7 errors

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You declare arrays with square brackets, []. So it's String[] not String before the String arrays.
 
Maneesh Godbole
Saloon Keeper
Posts: 11165
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are declaring is an array of String and not String.
Change to String wordListOne[]={..} or String[] wordListOne={...}
 
Sudheer Kolanu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you experts! I successfully run this program.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic