Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

random story in java (head first java book)  RSS feed

 
Sharlene Davis
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi im just beginning to learn how to write java just for fun a random story made by stringing phrases together im learning out of a text book made for java 5.0 and im using java 8.0
17 errors in the java compiler i was trying to use my own words aswell;


 
Campbell Ritchie
Marshal
Posts: 54903
155
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
That code looks familiar; is it out of Head First Java by Sierra and Bates? Sierra was the first person to set up this website, but we don't see her here very often.

Did you write that code with NotePad? Don't. NotePad is not at all good for programming; try some of the alternatives in our FAQ. My favourite on Windows would be NotePad++. Set up automatic indentation and conversion 1 tab&rarrow;4 spaces, and indent the code correctly. You have got the first line containing code which I would spread over 5 lines, so you cannot see what you are doing. Start by moving everything so each { is on its own line. Now delete the entire contents of the method and try compiling again; you should get no errors at all.

Now replace the first line and edit it so every comma is followed by a single space, but you will end up with a line too long, at which point you push enter after a comma and indent the second part of the line 2 indent levels more than normal. Now compile again.
Repeat the procedure for the second line in the method (which is the first place there is a syntax error). Again spaces after each comma and break the line into several lines. Also count the quote marks.

I tried compiling your code on Java8 (it should make no difference that you have Java5 code), and got 19 compiler errors. Don't change the code you originally compiled. You can only find errors if you post exactly the same code that you have tried. Otherwise you will get different errors and we shall be at cross purposes, and you cannot sort out the error with different code.
 
Campbell Ritchie
Marshal
Posts: 54903
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of tiny spelling errors like W/w and L/l. The compiler will interpret those as different letters and that is a potent source of errors. Do you have difficulty spelling? That can have a sting in the tail; you have managed to spell String Sting.
 
Knute Snortum
Sheriff
Posts: 3837
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You almost got the code tags correct.  They should surround the code.  I edited your post so you can see the difference.  Also, you can press the "Preview" button before the "Submit" button to see what your post will look like.
 
Sharlene Davis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your help guys sure is Head First Java by Sierra and Bates also i am using just using notepad pre-installed on windows as the book says to use i will try the others and see if that helps and thanks again for all the tips and tricks .
 
Sharlene Davis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ive only got 4 errors now woohoo doesnt seem to like the [rand2] syntax though im now using notepad ++



java.png
[Thumbnail for java.png]
 
Campbell Ritchie
Marshal
Posts: 54903
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please copy and paste the error messages because the screenshot is illegible. I have broken the long lines because they are illegible too. You indentation is inconsistent; you need to indent the code correctly so you can read it yourself.
I am going to make an unusual suggestion: Get an IDE. One is Eclipse; it marks the compiler errors with red symbols. If you click the red symbol, it will give suggestions for corrections. Some of the suggestions will be incorrect, but some will be helpful. In line 32, for example, it will give the option to change Sting to String.

[rand2]??? You have forgotten to write the name of the array before [rand2]
 
Lisa Austin
Ranch Hand
Posts: 141
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharlene Davis wrote:thanks ive only got 4 errors now woohoo doesnt seem to like the [rand2] syntax though im now using notepad ++






Hi,  I'm still learning myself so I hope it's okay if I pop in and point something out .  Head First is one of the books I'm using to learn.
I noticed you still have spelling errors.  Example is line 32 you have String spelled as "Sting".    One lines 31 - 35 "Length" should be in lower case.   Line 35 also has a misspelled word.   So try and follow the example exactly as it is. 
 
Sharlene Davis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks
 
Sharlene Davis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would copy and pasting have something to do with it as well?
 
Sharlene Davis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks i will download eclipse and add the array thanks
 
Knute Snortum
Sheriff
Posts: 3837
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This line have four problems.  Have you spotted them all?
 
Walter Gómez
Greenhorn
Posts: 11
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something else to take into consideration is the format of your code, because if you're able to read the code doesn't mean that other people will see it as you do. Robert C. Martin on his book Clean Code, provides details on coding standards and good programming techniques. If you have the chance get a copy of that book, go ahead and give it a try.

Now - as Knute mentioned the line of code he highlighted contains the errors you're getting from the compiler.

Happy coding!
 
Lisa Austin
Ranch Hand
Posts: 141
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
This line have four problems.  Have you spotted them all?


I'm curious .  Since I'm still learning myself.  In this line I see two clear problems in that actual line of code.  You say there are four.  I don't want to take anything away from the OP but could you tell me is the other two problems in relation to the code above?  Is so I see three problems different problems there.  I just can't figure out the other two problems .

I can wait for your answer to the OP  .  I'm just trying to figure out myself. 
 
Campbell Ritchie
Marshal
Posts: 54903
155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lisa Austin wrote:. . . In this line I see two clear problems in that actual line of code.  . . .
There are two errors in that line but the compiler produces three error messages for it:-
  • 1: Sting
  • 2: Name of array missed out before its [index]
  • 3: wordList3 declared earlier as WordList3 so you get a can't find variable error.
  • I tried it with Eclipse. The errors came up with a red mark and when I clicked the red mark, I got a dropdwn list of options. In most cases, one of the options had a green arrow and “Change to...” amongst the suggestions for correcting the error. The only error in that class which couldn't be corrected by clicking a “Change to...” option was the missing array name.

    I believe variable names with numbers in are bad: it is very easy to confuse wordList1 with wordList2 because the human eye doesn't see numbers as clearly as words and the human brain doesn't think in numbers as clearly as words.
     
    Lisa Austin
    Ranch Hand
    Posts: 141
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Lisa Austin wrote:. . . In this line I see two clear problems in that actual line of code.  . . .
    There are two errors in that line but the compiler produces three error messages for it:-
  • 1: Sting
  • 2: Name of array missed out before its [index]
  • 3: wordList3 declared earlier as WordList3 so you get a can't find variable error.
  • I tried it with Eclipse. The errors came up with a red mark and when I clicked the red mark, I got a dropdwn list of options. In most cases, one of the options had a green arrow and “Change to...” amongst the suggestions for correcting the error. The only error in that class which couldn't be corrected by clicking a “Change to...” option was the missing array name.

    I believe variable names with numbers in are bad: it is very easy to confuse wordList1 with wordList2 because the human eye doesn't see numbers as clearly as words and the human brain doesn't think in numbers as clearly as words.


    Gotcha.  I had saw those   ( yea me ) .  There is one more I saw which is for the declaration of the variable rand1 the word "oneLength" is misspelled as "oneLenth". 

    Thank You Ritchie!  I hope it was okay that I got involved and asked.  I'm trying to learn and so I read through all the beginning java questions.
     
    Knute Snortum
    Sheriff
    Posts: 3837
    91
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The fourth error is me thinking there are four errors (ooo, recursion!)
     
    Campbell Ritchie
    Marshal
    Posts: 54903
    155
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lisa Austin wrote:. . . the word "oneLength" is misspelled as "oneLenth".
    That was on a different line; I was only counting them on that one line. I am a Cockney, so I can see oneLenth as the phonetic spelling
    Thank You Ritchie!  I hope it was okay that I got involved and asked. . . .
    That's a pleasure the more questions you ask the more you will learn.

    Now, by searching this forum or otherwise, find a way to get pseudo‑random ints starting 0 and all less than the length of that array, better than using Math#random.
     
    Lisa Austin
    Ranch Hand
    Posts: 141
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Lisa Austin wrote:. . . the word "oneLength" is misspelled as "oneLenth".
    That was on a different line; I was only counting them on that one line. I am a Cockney, so I can see oneLenth as the phonetic spelling
    Thank You Ritchie!  I hope it was okay that I got involved and asked. . . .
    That's a pleasure the more questions you ask the more you will learn.

    Now, by searching this forum or otherwise, find a way to get pseudo‑random ints starting 0 and all less than the length of that array, better than using Math#random.


    Sorry I'm so late responding.  Would you be referring to the Random class? 
     
    Knute Snortum
    Sheriff
    Posts: 3837
    91
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, the Random class.  I believe the thread he's referring to is here.
     
    Campbell Ritchie
    Marshal
    Posts: 54903
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, but you will find lots more information in this forum. I had a look and found this which has lots of discussion about how to get pseudo‑random numbers, and some pitfalls which people have fallen into.
     
    Campbell Ritchie
    Marshal
    Posts: 54903
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:Yes, the Random class.  I believe the thread he's referring to is here.
    It is. But you neat me to it, gettin gthat quote. There is only one problem about that thread: it contains too many of my posts.
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:The fourth error is me thinking there are four errors (ooo, recursion!)


    The fourth error was that there was an error in the first place ;-)
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!