aspose file tools*
The moose likes Beginning Java and the fly likes method help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method help" Watch "method help" New topic
Author

method help

Steve Paj
Greenhorn

Joined: Nov 24, 2009
Posts: 12
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

Joined: Mar 01, 2010
Posts: 55
Try this one.
Steve Paj
Greenhorn

Joined: Nov 24, 2009
Posts: 12
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

Joined: Jun 02, 2009
Posts: 182

Hi Steve Paj,
try like this


A.A.Anbarasu
Steve Paj
Greenhorn

Joined: Nov 24, 2009
Posts: 12
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

Joined: Mar 10, 2008
Posts: 1821
    
    7

You could accomlish this using a while-loop and a continue.


Edit: Ugh, label was obviously unnecessary.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Steve Paj
Greenhorn

Joined: Nov 24, 2009
Posts: 12
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

Joined: Mar 10, 2008
Posts: 1821
    
    7

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

Joined: Nov 24, 2009
Posts: 12
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

Joined: Mar 10, 2008
Posts: 1821
    
    7

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

Joined: Nov 24, 2009
Posts: 12
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jul 23, 2008
Posts: 7
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

Joined: Oct 13, 2005
Posts: 39791
    
  28
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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1821
    
    7

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

Joined: Oct 13, 2005
Posts: 39791
    
  28
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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 24, 2009
Posts: 12
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

Joined: Oct 13, 2005
Posts: 39791
    
  28
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
 
subject: method help