This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes enter a value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "enter a value" Watch "enter a value" New topic
Author

enter a value

Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
Ok lets say I have several dialog boxes popping up continuously asking a user to input a value. The dialog box says to input a value or input -99 to exit. My question is if they input -99 how do I tell it to exit completely? Right now I have the -99 down in my if else statements that says:
*** that is the part right there were i am stuck. I don't know what to input to make it know to exit.

Thanks in advance.

[ edited to remove long unbroken comment from code block -ds ]
[ September 20, 2004: Message edited by: Dirk Schreckmann ]
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
System.exit(0);

But you should probably try to read some swing tutorials.

P.


Get a better web browser:<br /><a href="http://www.mozilla.org/products/firefox/switch.html" target="_blank" rel="nofollow">http://www.mozilla.org/products/firefox/switch.html</a>
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Try using
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
ok i did that...but for some reason its not working. i input -99 and the message in the dialog box says -99 is not a leap year and then i hit ok and it closes. why is it doing this and not exiting automatically?
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Without seeing the rest of the code ......
Maybe the :

bit is not working because value is not an int ?? If you've taken input from the keyboard, presumably the datatype is String / StringBuffer / array of chars (I dont know which ??)
If this is the case, maybe your if should be more like:


In respect of making the application quit when the user does enter -99; I was always taught that you should use formal structured flow control. So, I would write something like: (not in Java syntax, but you'll get the gist)



Hope this helps

Nathan
[ September 16, 2004: Message edited by: Nathan Russell ]
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
Here is my code...i meant to put that on my last message. sorry about that:

Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Hi,

OK, I've seen your code - my comments about

are unfounded ! My appologies

As to why the logic does not work as you expect; look at the flow and logic of your if's. Try changing the way you indent your code, and you might see it a bit easier.
Basically, its going down the road of:
Failing the first if (if (value % 400 == 0)), and going into the next else if (if (value % 100 == 0)). Its failing that also, and moving into the next else if (if (value %4 ==)). Its failing that, and passing into the else block (IE. the else block for that if) - which is setting message to -99 is not a leap year.

Your enclosing while block is correct, and exactly what I would do - but I'd definately look at the Java equivilant of Select/Case to replace all those nasty if's !!

Hope this helps

Nathan
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
first off thank you for the help!! i appreciate it! so you mean they are out of order? like maybe i should have my -99 first, then the leap years and last non leap year if's. if i read that wrong sorry.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Originally posted by Nathan Russell:
I'd definately look at the Java equivilant of Select/Case to replace all those nasty if's !!
[/QB]


Nathan means the switch statement.
Here is the relevant section from the java tutorial.
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
so am i not allowed to have System.exit (0) in an applet? to me this is confusing, b/c what other way is there to close out the program when a user puts in -99. BTW thank you for the link im going to work on that. easier to read that way for sure.
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Hi Holly,

Its not that you are not allowed to use a System.exit(0) - I'm just saying that I wouldn't.

I was brought up / taught programming through a very formal route. I was taught Pascal, not cos its a great language; but more its a great teaching language. Its very very structured, and you can't do things like goto (dont think you can do that in Java), bailing out of loops early (I know you can do this in Java - IMHO yuck !), terminating programs early (like you are trying to do) etc. The statements and commands are simply not there, so it forces you to design the logic and flow of your program before you hit the keyboard.
(From memory, there was an equivilant of System.exit(0) in Pascal, but the only time you would use it was once at the very end of your program, and its use would be to return an error level. Thats the only time we were ever allowed to use it; and our lecturer would severely mark us down for any other use - certain if we did what you are trying )

Don't get me wrong, I'm not trying to discredit what you are trying to do - I'm just saying that there are other ways, and that personally I would use an enclosing while with a switch/case block within it.

All the best

Nathan
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
Ok I see what you are saying. I understand that and I agree. I think my prof is a little loopy sometimes, because that is what he is wanting us to do and when I figured that out I was like well in a way thats kind of wrong to do, but hey I better figure it out to get the good grade right Thanks for the help...much appreciated.
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
ok is there a way to have the dialog box close that the user is using...not the whole applet just the dialog box. is that possible?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I have some comments:
a)
This isn't a valid code at all:

b) I would like to see how the Nathan-code would look if made with a switch/ case-statement. (not really)


http://home.arcor.de/hirnstrom/bewerbung
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

Holly,

does the code you posted even compile? the way it's written, it looks like you'd get an "else without if" error.

A simpler exaple of what you have (taking out the else-if's) is


this isn't legal syntax. you can only have one ELSE for a given IF/IF-ELSE.

The test for whether or not to quit the program (value == -99) is really independant of leap-year test. what you need is a test for (should i do the leap-year test OR should i quit the program).



if you wrote your code this way, you could put in little stubs that fake the missing parts.

you would be able to see if your code for getting the input works, and if you simply wrote println statements that said "i should quit" and "i should test leap year", you could see if that logic worked right.

note that the above suggests a way to avoid the exit(0) in the middle of the program...


now you can replace the comment i have with code for testing the input year.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
I'm not sure if you were asked to perform the maths to work out if a date fell into a leap year, because if not, a method exists in the java.util.GregorianCalender class that you might be interested in.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enter a value
 
Similar Threads
Binary to Decimal conversion
JVM encoding param to Tomcat Startup
HOWTO check for click of Cancel button
Convert decimal value to binary
Nested IF's