• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Need help fixing an Error with my current code for an assignment

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello this is my first post here! I was working on an assignment today concerning while loops. Here is the prompt to be specific, " Write a flag controlled while loop that will convert an input word to pig latin. The flag should be set to false when the word “end” is entered and no pig latin word displayed. You may assume that the statement pigLatin = translate(word); will convert the input word to pig latin correctly. The code for the input of the initial word is provided. You may assume that at least one word will be translated. "
My current issue is that, when I run my code it runs correctly for the first word I input, but after that it prints the pigLatin for only that first input and none of the new ones I feed it. Here are the outputs I've been receiving to clarify what I mean
Enter the word end to stop the next program!

Input word: dog

Input word: dog
Input word in pig latin is ogday
bread

Input word: bread
Input word in pig latin is ogday
bro

Input word: bro
Input word in pig latin is ogday

Here is my current code.

 
Marshal
Posts: 3260
487
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!  

You are entering three different words.  Are you calling your translate method for each word?
 
Michael Ameer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Welcome to the Ranch!  

You are entering three different words.  Are you calling your translate method for each word?



The purpose of the code is to turn every entered word into pigLatin, (the method that turns those words to pigLatin was already created by my teacher at the bottom of the assignment), However as you can see it is only turning the first word into pigLatin.
 
Ron McLeod
Marshal
Posts: 3260
487
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Ameer wrote:... However as you can see it is only turning the first word into pigLatin.


Yes - understood, that's why I asked if you are calling the translate method for each word.  If you are unsure, maybe add a print statement to the translate method to verify that it is actually being called and show the word which is being passed-in for translation.
 
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at your code again and see how many times you're assigning a value to the pigLatin variable.
 
Michael Ameer
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Look at your code again and see how many times you're assigning a value to the pigLatin variable.



THANK YOU SO MUCH!! IM SO UPSET I DIDNT NOTICE THAT AFTER STARING AT MY CODE ALL DAY
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't feel bad, it's a common phenomenon and you'll experience it quite a few times as you learn. It's a form of tunnel vision. One way to overcome it is to actually stop staring at your code for a bit. Take a break, go get a snack or a cup of coffee, watch a little bit of TV or read a news article, something unrelated to the problem you've been working on. The break allows your subconscious brain to take over and do some background processing. When you get back, your brain will have a chance to see the code with fresh(er) eyes.

Another way to get around tunnel vision is to rubber duck your code. This means you try to explain your code to an imaginary programming partner. You can use something like a rubber duck as a stand-in, hence the name of the technique. I use a little rubber Star Wars character, of which I have three: Darth Vader, Chewbacca, and Boba Fett. I also have the Go Gopher. Trying to explain your code out loud allows you to bypass any "short-circuits" that have formed in your brain that prevent you from seeing the bug.

I think the short-circuit that was in your brain in this case was from line 7. I suspect you might have thought that line did something useful when in fact it doesn't. You could safely delete that line and still not change the way your program worked.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic