Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

empty input with JOptionPane (how to catch it ?)..

 
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a question, i have a program where the user creates a box or more boxes, and they are saved during runtime only, in an Array.(that was how i had to write it for a project.).
no matter where in the program if i use an input dialog like---> and i press OK with nothing in the input box,all the boxes are deleted that where made?? My question is how do i catch it and get the user to re-input?I see with a String i can use if(id==null),but how do i do it with an integer?


Thanks
Mike
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the javadoc and should be able to figure it out.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Wouter!!

I didn't think it would be that simple;), now just gotta figure out how to loop back to the input again..,This wasn't actually part of the project but i noticed it and wanted to correct it.
I hope we do exceptions next as that seems to be a very important part of programming, and there are tons of other errors that can eventually happen i suppose.

Mike
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About the re-input question.

You can reverse the question to come up with the answer.

If you user inputs a wrong value ask again
Becomes
Stop asking the user for a value as soon as he enters a correct value.


 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Wouter,

Yes i am using while(input==wronginput){try{, it just looks like i will have to re-do the loop a bit so after the catch(NumberFormatException) it goes back to it.Someone also wrote here that i shouldn't use continue; in a loop,

why is that exactly.Or what is the reasoning behind it?And if i don't use that then how else is it done?for instance if someone enters a "zero" and i have if(id==0){continue;}
and thanks once again for the help;)

Mike
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The usage of break and continue should be discussed in another thread because a lot of people will have a strong opinion about it.
And you don't even have to use break:


 
Marshal
Posts: 79635
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And avoid == on reference types.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

ok then i can just use boolean true;false; I was actually using a boolean and break;continue; ok then i will use that method!!


Thanks!
Mike

should i start another thread about the use of break and continue?

(although i just read via google , that a while statement MUST always have a break; ? )i suppose google isn't always your friend...
 
reply
    Bookmark Topic Watch Topic
  • New Topic