• 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

enter a value

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.exit(0);

But you should probably try to read some swing tutorials.

P.
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using
 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code...i meant to put that on my last message. sorry about that:

 
Nathan Russell
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic