Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PhraseOMatic syntax problem in Head First Java

 
j rich
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep getting loads of errors when trying to compile this, does anyone have an idea what's wrong? I copied it word for word, dot for dot from the book, and I believe it's supposed to compile.


Any help?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general it might help if you list what the error messages actually are.

In this case, though: you've missed the '{' after 'public class PhraseOMatic'.
 
j rich
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's still showing errors after I've added the curly braces.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What errors?

Edit - actually, quick guess: probably something about String literals not being closed? Don't split your strings over two lines - e.g. "multi-Tier" (if that's exactly how you've formatted it in your program).

You really do need to give the error messages when asking this sort of question, though (see TellTheDetails).
 
j rich
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that fixed it, thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please understand that simply saying "It doesn't work" is useless. So useless, in fact, we have a FAQ for it: ItDoesntWorkIsUseless. So after someone says that it'd help to include *what* errors, to immediately not include the errors *again* isn't the most efficient way to get help. Make it easy for the volunteers who spend their time here to help!

Thanks!
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look around-Processor speed.......wen i execute i keep getting the first element of array is combined with println() function..... hmmmm
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Pie
Posts: 49772
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridharan wrote:Take a look around-Processor speed.......wen i execute i keep getting the first element of array is combined with println() function..... hmmmm
What does that mean? Don't understand.
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


wen i execute i keep getting linkin park is the best
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clearly a design flaw, as of the three performers listed, obviously Clapton is the best.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridharan wrote:

wen i execute i keep getting linkin park is the best


Well first off this doesn't execute, it causes a compile time error.
The reason you are always getting linkin park is the best is because you're doing this:



This will always evaluate to 0, because Math.random() returns a double less than 1, and when you cast to an integer the right hand side is truncated. So you always end up with 0 * i, which is 0.

I think you meant to do this:



Hunter
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you..... This solved it.......
 
Aman Kulkarni
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class PhaseOMatic
{
public static void main(String args[])
{
String[] wordListOne={"24/7","multi-tier","30,000

foot","B-to-B","win-win","frontend","web-based","pervasive","smart","six-sigma","critical-path","dynamic"};
String[]

wordListTwo={"empowered","sticky","value-added","oriented","centric","distributed","clustered","branded","outside-the-box","

positioned","networked","focused","leveraged","aligned","targeted","shared","cooperative","accelerated"};
String[] wordListThree={"process","tipping-point","solution","archtecture","core

computing","strategy","mindshare","portal","space","vision","paradigm","mission"};
int oneLength=wordListOne.length;
int twoLength=wordListTwo.length;
int threeLength=wordListThree.length;
int rand1=(int)(Math.random()*oneLength);
int rand2=(int)(Math.random()*twoLength);
int rand3=(int)(Math.random()*threeLength);
String phrase=wordListOne[rand1]+" "+wordListTwo[rand2]+" "+wordListThree[rand3];
System.out.println("What we need is a "+phrase);

}
}



This program is 100% error-free and will give correct output...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aman Kulkarni wrote:public class PhaseOMatic


First, why are you resurrecting a year-and-a-half-old thread?

Second, don't do others' work for them. This site it NotACodeMill (⇐link).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49772
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please make sure to indent your code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic