• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

While Loop Error

 
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am trying to convert input to pig latin but after the word has been translated, the loop will not restart.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Don't use == and != to compare the values of two Strings. That only compares whether they are the same object. Use the equals() method instead -- and the ! operator if you want not-equals.
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to use the != because I need to make sure the code checks that pigword doesn't equal to end. If I use the equal method, I won't be able to run the code in my while loop.

 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

regg jones wrote:I have to use the != because I need to make sure the code checks that pigword doesn't equal to end. If I use the equal method, I won't be able to run the code in my while loop.]



Re-read Paul's reply - he did comment on that case:

Paul Clapham wrote:... Use the equals() method instead -- and the ! operator if you want not-equals.

 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Ah there we go, I just had trouble placing the exclamation mark, I have one more question now. This code is part of a bigger project. After the pig latin code has run, it doesn't run again nor does it run the next set of code. Here is my project code below.

#2
   //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 = pl.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.
   
   
 // #3
   //Write a while loop that will count the number of even numbers entered.  The loop should stop when 0 is entered and 0 should not be counted.  You may use any form of while loop you desire. The code for the input of the initial number is provided.
 
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand the while-loop in the solve() method. There's nothing in that method which can change the pigword variable to "end", so the while-loop can only run at most once.

If you want to call the solve() method more than once, you need to write some code which does that. A while-loop would certainly be a good choice.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I don't understand the while-loop in the solve() method. There's nothing in that method which can change the pigword variable to "end", so the while-loop can only run at most once.


Based on that description, my concern would be that it would be stuck in an infinite loop, rather than running at most once.  But there is also a return within the method, allowing the method to complete.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . . There's nothing in that method which can change the pigword variable to "end", so the while-loop can only run at most once. . . . .

Surely that means that is an infinite loop and it will never terminate. Actually, as you suggested, one shouldn't have a loop inside the method; one should have the method call inside a loop.Please avoid \n and \r; despite what you see in many books, they don't necessarily give the correct platform‑specific line ends. I don't think you need a line end there anyway.
Line 9 will only be executed after your user has entered 0.  You aren't testing that the input is an even number in line 6. The variable name evenNumber is confusing. I am sure you can think of something better.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Based on that description, my concern would be that it would be stuck in an infinite loop, rather than running at most once.  But there is also a return within the method, allowing the method to complete.



Good point. So I understood the loop even less than I thought I did. But it looks like the loop is infinite only if you give it a word with no vowels.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:. . . there is also a return within the method . . . .

I missed that: sorry.
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright I tweaked my code a bit, apparently I deleted some code at the bottom of the project file that my teacher provided for me. Now I have an endless while loop.

// #2
   // 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.

 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. Your while-loop covers lines 7, 8, 9, and 10. It repeats those lines until something changes the value of the variable pigword to "end". Now, which of those lines changes the variable?
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want the code to be able to ask the user to type in another input. But my code doesn't do that. It just asks it infinitely.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Your while-loop covers lines 7, 8, 9, and 10. It repeats those lines until something changes the value of the variable pigword to "end". Now, which of those lines changes the variable?



Then try answering this question.
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh, I have figured out the problem now. But for my prompt 3, I am trying to check if the number is divisible by 2 and it keeps popping up with an error.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please have another look at my post and see where you need the test. You can use the % remainder operator to test for even numbers, but you have only written part of the expression. Work out what the result of using % 2 is and how you would convert it to true/false.
Why are you printing anything in line 7?
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have changed my code a bit more and now I cannot seem to figure out a way to make the number variable equal to the scanner input. Here's my code.


 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

regg jones wrote:I have changed my code a bit more and now I cannot seem to figure out a way to make the number variable equal to the scanner input. Here's my code.



Line 9 in your posted code does that. So perhaps you could explain why you don't think it does that.

I would also recommend indenting your code properly; it's hard for me to understand that code the way it's indented so it's probably even harder for you to understand.

 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that because I try running the code and I get the following error.

Main.java:77: error: incompatible types: String cannot be converted to int
   number = scan3.next();
                      ^
1 error
compiler exit status 1
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. I had assumed you had already compiled the code correctly and were having problems running it. So I failed to notice that you didn't use the Scanner method to get the next integer from the stream. Funny, you did that before but not here.
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way I can convert the scanner input to integer?
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read input as any kind of signed integer (small i). Look at the methods of Scanner.
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I used the NextInt code but now I want to make int number= 0 and also = scan3.nextInt()

Here's my updated code below

 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

regg jones wrote:ut now I want to make int number= 0 and also = scan3.nextInt()



Could you explain what you mean by that?
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally fixed the code now, here's my result


 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent! There were several things which didn't make sense in the earlier versions but it looks like you took care of all of them.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you get that line 3 to compile? The 0 is a “value” and you are only allowed “variables” to the left of an assignment operator =.
Are you trying to give number two different values simultaneously?

[addition]I see you have corrected that problem
 
reply
    Bookmark Topic Watch Topic
  • New Topic