Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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:


 
Campbell Ritchie
Sheriff
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic