• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Newbie "This Old Man" attempt with For Loop and Switch Statement  RSS feed

 
Jb Anderson
Greenhorn
Posts: 13
Eclipse IDE Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Again. To reiterate, I am a newbie attempting to learn Java through iTunes Stanford CS106a and Eric Roberts' "Art and Science of Java." I am not a student nor am I enrolled in any class. I am attempting a programming exercise (chp. 4, ex. 2) which relates to the children's rhyme "This Old Man" where each verse number (displayed at the end of the first line) rhymes with the final word of the second sentence. As a refresher course: "This Old Man, he played 1. He played knick-knack on my thumb. With a knick-knack, paddy-whack. Give your dog a bone. This old man came rolling home." I have incorporated a "for loop" to increment the verse number (displayed at the end of the first sentence of each verse) and I have used a "switch statement" to plug in the associated rhyming word (at the end of the second sentence of each verse). Also, I have added an additional "println" at the end of the code to provide proper spacing between each verse when displayed. However, I can't figure out why my "switch-statement" plug-in word is appearing on its own line.

If I append the code:

println ("He played knick-knack on my " + i);

it plugs in the "for loop" number and not the switch clause. It would be ideal to create an additional "for loop" or variable "n" for the switch statements and make it println ("He played knick-knack on my " + n);

...but, I believe from what I have read so far that might be illegal syntax.


So, I feel perhaps I might be missing a concept. As usual, any feedback would be appreciated. Thank you for helping me.




 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing line 19 from a "println()" statement to a "print()" (<-- note the removed "ln") statement should fix the problem.

Think of println() as print with a new line at the end where as print() does not add a new line at the end.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You program outputs:

This old man, he played 1.
He played knick-knack on my
thumb.
With a knick-knack, paddy-whack.
Give your dog a bone.
This old man came rolling home.

This old man, he played 2.
He played knick-knack on my
shoe.
With a knick-knack, paddy-whack.
Give your dog a bone.
This old man came rolling home.


This is really close. You have two options. One is what Scott suggested - to use a print() so you aren't printing a new line there.

The other option is to store the word in a variable and print out the lines from the song after you know what goes there:

 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just something to think about, you might be able to use an array rather than a switch statement.

Create an array[] to hold each rhyming word. Then on line 19 concatinate the String from array location i into the println() statment.


Not sure if the assignment specifies using a switch statement or not but and array can be a good alternative.
 
Jb Anderson
Greenhorn
Posts: 13
Eclipse IDE Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott,

Thank you for your speedy reply. I appreciate your taking the time to review my query and my code and offering assistance.

The "print" instead of the "println" is good to know for the future. The array option also seems like a logical method as well. Unfortunately, neither of those topics have come to light yet. Chapter 4 (from where my example is taken) primarily focuses on "Statement Forms" that include: single-line if, multi-line if, if-else, cascading if, switch, for loop, and the while loop.

Based on the book topics, I originally felt a "nested loop" would be effective. Yet, Eclipse gave me all types of syntax errors. Which is why I felt an "if" and a "switch" might be successful. And, while my results are really close it does appear that I missed a concept or am not integrating the correct methods.

 
Jb Anderson
Greenhorn
Posts: 13
Eclipse IDE Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne,

Thank you for taking the time to offer some assistance and direction with my query.

Your suggestion of the strings, is something that hasn't been addressed in the book yet. However, it TOTALLY is along the lines of what I was trying to accomplish when I was thinking of creating another variable. But, that was when I ran into Eclipse continually barking at me with errors. I did go back to my code and unsuccessfully tried using "name" as the variable in the switch statement:





I will go back to the woodshed and see if I can craft an alternative within the methods discussed so far in the book.

Again, I appreciate your help.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jb Anderson wrote: I did go back to my code and unsuccessfully tried using "name" as the variable in the switch statement:

Can you show the full code using name? Even if you don't end up using that approach, we can let you know why it didn't compile. Then you'll have learned something extra as a bonus.
 
Jb Anderson
Greenhorn
Posts: 13
Eclipse IDE Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Thank you again for your assistance in helping me learn.

So, as previously mentioned, changing my code using the "name" as the variable did not work. But, after re-examing your suggestion of the "string array" (which I have not learned about yet) I realized I *only* changed the name in the switch statement. Because of my unfamiliarity with the technique, I realized I did *not* add the line of code for (initializing???) the string array with the "string name = null;" Obviously, the code compiled successfully after I corrected that omission.

Thank you again for taking the time to help me and to follow up. I greatly appreciate your taking the time and effort to help me learn to code.

And, as requested, here is the complete code with your suggestion of the "name" string….


 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!