This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Phrase 'O Matic Identifier Error

 
Greenhorn
Posts: 4
Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I'm new here but joined because everyone seems nice and knowledgeable.

I'm trying to learn Java, so I have started working through the Head First Java book and came to the Phrase O' Matic program. I've seen posts on here about this program, but not for my specific issue.
Google searches didn't bring up my specific problem, or clues enough for me to problem-solve on my own, either. I apologize if I missed something in my search since yesturday, but I did try to find this before posting.
Following is the code, written just as it is in the book, sans returns since I was under the impression that you are supposed to let the program word-wrap and not do it for the IDE in Java (please correct me if I'm wrong ).



This code gives me these errors:




I honestly have no clue what I am doing wrong, and I don't know what identifier is needed, nor why it is expecting ')' in odd places. I am running VS Code on Linux Mint, if that helps as well. I have the JDE from Oracle installed as well as Mono.
Thanks in advance for any advice you can give as to where I went wrong! If this seems to be a duplicate thread because I missed something, please feel free to let me know and direct me to the thread, as I didn't see it on my first stroll through the search.
 
Saloon Keeper
Posts: 6227
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brittany Taylor wrote:Hi there, I'm new here but joined because everyone seems nice and knowledgeable.


Welcome to the Ranch, Brittany!

You'll find many people here eager to help you find answers and staff, whose job is to make everyone's experience here a positive one, doing everything they can to ensure you're treated with patience and respect.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than using Math.random() like that, you can use the java.util.Random.nextInt(int bound) method. Here's an example of how to use it:
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, there's a lot of duplication in the code you wrote. Have you been taught how about methods yet? Because you can write a method like this:

With this method, you can replace this code:

with just this one statement:

That is much more readable because it hides a lot of the implementation details in the private randomPhrase() method.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brittany Taylor wrote:Following is the code, written just as it is in the book, sans returns since I was under the impression that you are supposed to let the program word-wrap and not do it for the IDE in Java (please correct me if I'm wrong ).


Ah, so you copied this straight from the book... well, as Corey said, you used the wrong characters for creating an array literal.

I'm not clear on what you mean by "let the program word-wrap" and "not do it for the IDE" --

If I were coding, I might take this line and the other two like it

and format it to something like this:

The one thing to watch for if you manually format code though is that IDEs like Eclipse and IntelliJ IDEA have a feature that autoformats the code for you. The code formatter in those IDEs are configurable but they're also very opinionated. Any effort to format code manually in an IDE can quickly go to waste once someone uses the Format Code command. I don't know if the book says something along these lines or not but when it comes to formatting code, I usually just accept the IDE's formatting. In cases where I do want some custom formatting that conflicts with the IDE's built-in formatting rules, Eclipse and IntelliJ can be configured so that it will leave sections of code as-is when it is autoformatting code.

This is how you can preserve special formatting in Eclipse and IntelliJ IDEA:

There is a configuration item you have to set for this to work though.
 
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure, but I suspect the Phrase'O'Matic has some relics of old code (pre‑Java5) before printf() was available.
And again, BT: welcome to the Ranch Look at the authors of your book. Kathy Sierra was instrumental in setting up this website, a long time ago.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Kathy Sierra was instrumental in setting up this website, a long time ago.


She quite literally was the one who set it up. See A History of JavaRanch
 
Brittany Taylor
Greenhorn
Posts: 4
Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really appreciate the warm welcome and help! I had to go to my 9-5, so I didn't want to reply until I had a chance to try what was suggested .

Carey Brown wrote:Lists should be enclosed in braces {...} and not parenthesis (...)


This fixed it! Thanks, looks like it was the preferable user typing error compared to me not installing my dependencies in Linux right... whew!

Junilu Lacar wrote:Have you been taught how about methods yet? ... I'm not clear on what you mean by "let the program word-wrap" and "not do it for the IDE" -- ... IDEs like Eclipse and IntelliJ IDEA ...


Not too much on the methods, no. I have fiddled with Java in the past but never in advanced enough of a way to draw from that knowledge now with confidence or from memory.  I was floating between coding languages for longer than I should have been. Was going to go C# before Java, but Java is easier to set up across all of my platforms so Java it is lol. Plus, Java and C# have plenty in common if I want to actually jump from Java over at a later time once I've learned enough to be proficient.
For the word-wrap portion, with some coffee to help me think I can see that I misunderstood the quote out of the book to mean not to hit return while defining any statements, but it really just meant not to hit return in the middle of a String. Which... duh I should have been able to piece that together hahaha. I'm going to blame the evening pre-Monday blues for that one :P.
That stuff is good to know about with the editors, though, I do appreciate that information. VS Code has been good to me in formatting thankfully, but I'll keep that in mind if I want to move to a different IDE to try it out at some point.

It's really rad that I ended up on one of the author's site without intending to, though! Is she still active here? And despite this book being Java 5.0 (and the most current edition that I'm aware of) compared to the Java 12 that is out now, I still don't see a book as highly recommended for learning the stepping stones of a language. Props for that! I debated between this one and Thinking in Java, but that seemed a bit dense for hopping back into the learning game. I took a break from coding for school relating to medical coding, but now that I'm working in that field now and out of school I figured it was time to start learning the coding I'm more interested in.
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brittany Taylor wrote:. . . Is she still active here? . . .

I haven't seen her for ages, but Bert Bates still occasionally posts here.

Thinking in Java . . .

(=TIJ) Another good book; I liked the 2006 edition, but didn't like the more recent Java8 edition, however. You will find TIJ moves much faster than HFJ and goes to a much more advanced level.
 
Brittany Taylor
Greenhorn
Posts: 4
Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: I liked the 2006 edition, but didn't like the more recent Java8 edition, however. You will find TIJ moves much faster than HFJ and goes to a much more advanced level.



What didn't you like about the more recent version, if you don't mind my asking?

But I definitely got the impression that TIJ was well written and had good info, but I felt like if I didnt go into the intermediate stuff of that book without at least a small foundation I'd have a harder time clicking the concepts together. Could be just that I am over-thinking the issue,  too lol.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I can tell, On Java 8 is not much different from the prior Thinking in Java books. The first few chapters look more or less exactly the same. Of course, there will be material that deals with language features that were introduced in Java 8. (Caveat: I bought the OJ8 book and downloaded the TiJ 4th Ed. book but haven't gone through in detail. My opinion here is based on cursory glances at the table of contents and flipping through pages quickly)

I do think that Eckel's books could be a bit daunting for beginners so if you're not a hardcore learner and prefer a gentler introduction, there are other books folks here tend to recommend.
 
Marshal
Posts: 7178
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brittany Taylor, welcome to the Ranch.

Few comments on naming.

1. Class names supposed to start with an upper case. So in your case that would be PhraseOmatic.

2. When you declare an array, do not name the variable which includes something sounding like List (i.e. wordsList). List is an interface in Java, or simply think - yet another data type. So to eliminate confusion for your code reader whether it is actually an array or list, find some other variable name which tells the same, just does not include this detail.

Could they be replaced with:
  • wordList1 -> mixedWords ?
  • wordList2 -> adjectiveWords ?
  • wordList3 -> nounWords ?


  • Have you noticed that those List1,List2,List3 (noninformative distinction of variables) actually been replaced with something more meaningful (I was just slightly unsure about the first one, to me seems it contains mixed words, but that might be off, please find some better name for it).

    Now that you have something more meaningful, look how the process of building the phrase become more exciting:

    This is really how the sentences are being built, isn't it? out of nouns, adjectives, verbs...
     
    Brittany Taylor
    Greenhorn
    Posts: 4
    Python Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:Brittany Taylor, welcome to the Ranch.

    Few comments on naming.

    1. Class names supposed to start with an upper case. So in your case that would be PhraseOmatic.

    2. When you declare an array, do not name the variable which includes something sounding like List (i.e. wordsList). List is an interface in Java, or simply think - yet another data type. So to eliminate confusion for your code reader whether it is actually an array or list, find some other variable name which tells the same, just does not include this detail.

    Now that you have something more meaningful, look how the process of building the phrase become more exciting:

    This is really how the sentences are being built, isn't it? out of nouns, adjectives, verbs...



    Thank you for the information . The naming conventions are old habits, I'll do what I can to make them more standard. In terms of using the term 'List' in my variables, I was just copying the book, though I did not know about the List interface in Java, so I am glad to know that to kill any bad habits early on and appreciate the tip. I can see your point on using descriptive names as well, to help with recalling what each list functions for.

    Junilu Lacar wrote:...is not much different from the prior Thinking in Java books.


    That's good to know. Won't have to shell as much money if I go to buy that one in a while after working through Head First. I feel like I could get through it, but it'd take a bit more for me to grasp the concepts without a prior foundation. Might try a few chapters to see how I do, though. Sometimes a book can suprise you if you click with the author's writing style and tone.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    On Java 8 can only be bought as an ebook. The prior Thinking in Java books are available for download, for free.
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am afraid I can't remember what I disliekd about what I saw of Eckel's thrid book. Sorry.
    Get something like Java8 in Action or Modern Java in Action by Urma Fusco and Mycroft (those names are actually different editions of the same book) from Manning. Wait until they have a special offer of 50% off; that might happen on 31st October.
     
    When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!