Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method help

 
Steve Paj
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I have to write a method that will ask a user if they wish to continue.

The only ACCEPTED responses are Y/y or N/n. Anything else will reprompt.

Y/y returns true. N/n returns false.

My problem is that when compiled it wants a third return to accommodate anything other than Y/y or N/n.

Help please!

Here's what I have:



In the else statement I am returning false, but I cannot do that or the main program will not run properly.

Thanks!!!

PS this should be in a while loop probably, but I have the same issue.
 
Shinelin Samuel
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this one.
 
Steve Paj
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shinelin Samuel wrote:Try this one.


Thank you for your time and effort. This will not work because the 'else' statement cannot contain a condition...
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve Paj,
try like this
 
Steve Paj
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anbarasu aladiyan wrote:Hi Steve Paj,
try like this


Thank you for your time and effort. I have to return true (when Y or y) and false (when N or n) to main.

Main is asking a user if they want to continue with the program. So they either answer Y or y for Yes or N or n for No. If they enter anything else it asks them again if they want to continue. They can only type Y or y or N or n.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could accomlish this using a while-loop and a continue.


Edit: Ugh, label was obviously unnecessary.
 
Steve Paj
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:You could accomlish this using a while-loop and a labeled continue.


Jelle thank you very much that did the trick!
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, except for the labeled bit, that was a bit superflous I'm affraid.
Don't know why I even suggested it really, let's blame it on lack of caffeine and forget it ever happend...
 
Steve Paj
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:Yeah, except for the labeled bit, that was a bit superflous I'm affraid.
Don't know why I even suggested it really, let's blame it on lack of caffeine and forget it ever happend...


I already forgot.

I'm sure my instructor will complain about the label, but he probably won't take too many points.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, maybe you can compensate by trimming off some of the additional fat, the way I did in my edited post above ;)
 
Steve Paj
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:Yeah, maybe you can compensate by trimming off some of the additional fat, the way I did in my edited post above ;)


That is pretty. Thanks again for your help!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While it's too late now, I want to remind our answerers that we strive *not* to provide ready-made solutions, but rather guide the people asking the question in a way they can figure it out for themselves. had I seen this earlier I would have deleted the answers.
 
kohsa ashok
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:You could accomlish this using a while-loop and a continue.



Hey buddy, this code will work perfectly without even the "continue" and still meet all the requirements
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like all these continue and breaks and multiple returns. I would suggest you use a while loopThe while checks that you have neither Y nor N, and the single return statement looks to see whether it is "y". Using the literal before the . obviates the risk of input being null, which would cause a NullPointerException.You aren't doing anything in the loop, only using it as a check, so you must comment the empty statement. The extra () might be redundant.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also don't like break, continue (especially with a label - that's an ugly goto in disguise) and infinite loops, but Campbell's code also looks a bit cryptic to me.

Jelle, the continue you have on line 10 is unnecessary.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:I also don't like break, continue (especially with a label - that's an ugly goto in disguise) and infinite loops, but Campbell's code also looks a bit cryptic to me.

Jelle, the continue you have on line 10 is unnecessary.


Yep, somehow "Oooh, I know, a labeled continu will fix that!" popped into my brain yesterday, and in a moment of questionable clarity I noticed that the label was unnecessary, but somehow missed that the entire continu statement could be dropped. D'oh!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, Jesper. I do have this ability to write cryptic code. Doing FORTH programming, of course, makes me even worse at that . How would you have written it? How about this?Although the dot operator . has a higher precedence than the bang operator !, I have used redundant () to underline that precedence.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe with a do-while loop:

That doesn't require an infinite loop, break, continue or duplicating statements like input = myScanner.next(); like in your last example.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My vote was for a do-while; in this case it seems like the most logical choice.

Either that or a Forth re-implementation, because Forth is awesome.
 
Steve Paj
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EDIT: Nevermind. I'm a newb and I really had to think about it for a while. All is clear now. Thank you for sharing these alternatives with me. You have truly enriched my learning!


Thank you all for your comments. That is some very pretty work you all have done there. I'm not sure if the last few iterations will work for me, because I have to return true for yes, but I also have to return false for no. Will these iterations accomplish that?

Our instructor wrote the main program and when he calls the method, main is expecting either a true or false, and based on that, of of two things will happen.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't looked too closely at all the code submitted, but they will all (I believe) return true for "y"/"Y" and false for "n"/"N". Jelle's loop and mine will both repeat whenever you don't enter "y" or "n", so if you enter "maybe" it will simply repeat the prompt. If you enter "y" or "n" they test whether you have entered "y" (= true); the only other possibility at this stage is "n", which doesn't equal "y", so it returns false.

I won't try to Forcthe FORTH onto a Java newbie
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic