• Post Reply Bookmark Topic Watch Topic
  • New Topic

JOptionPane OK_CANCEL_OPTION help  RSS feed

 
Michael Smithy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code:


do{
....
}while ( |insert word| == true);

basically the code is suppose to run while the user did not press cancel on the window created by JOptionPane.
However, I do not know how to assign truth values to stuff like this... I'm still a noob ;-;

Also, if the variable is not true, aka the cancel button was pressed, the window will close with
System.exit(0);

HELP
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of the times the JOptionPan is used as follows

Hope it helps!
Kind regards,
Roel
 
Michael Smithy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Most of the times the JOptionPan is used as follows

Hope it helps!
Kind regards,
Roel

Thanks for replying!
I keep getting this error: Error: Type mismatch: cannot convert from java.lang.String to int
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Smithy wrote:I keep getting this error: Error: Type mismatch: cannot convert from java.lang.String to int

Without sharing the few lines of your code snippet which produces the above error, it's very hard to tell what's causing the issue and how to fix it.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Most of the times the JOptionPan is used as follows


Minor nitpick.
Always a good idea to use instead of
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:. . . Minor nitpick.
Always a good idea to use instead of
Why? Those fields are plain simple ints, and there is no risk of exceptions from the == operator. Surely you would go for readability which the version with result first gives you.
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Minor nitpick.
Always a good idea to use instead of


I must concur with Mr. Ritchie in not understanding Mr. Godbole's nitpick. JLS 15.21.1 (Numerical Equality Operators == and !=) doesn't mention any anti-commutative behavour, and no unboxing conversions are occurring.

Now the difference between x.equals(y) and y.equals(x) is a different matter. Choosing the correct order there is sometimes crucial.

update: originally referenced an irrelevant section of the JLS, oops.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many a times in the past, have I fallen for the classical trap

I knew getAge() was supposed to return an int, only get smacked by user being null. Since then I have developed this is a must have habit, sort of switching on my turn indicator even when I know the road behind me is empty. YMMV
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Many a times in the past, have I fallen for the classical trap

I knew getAge() was supposed to return an int, only get smacked by user being null.

But I think we can agree that if (20 < user.getAge()) would throw the same NPE, right?
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the static method showInputDialog() does not throw such Exceptions.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:I knew getAge() was supposed to return an int, only get smacked by user being null. Since then I have developed this is a must have habit, sort of switching on my turn indicator even when I know the road behind me is empty. YMMV

I wonder how switching both operands can avoid a NullPointerException being thrown at runtime when user is null. The only thing you can do is add another condition (user != null) and use the short-circuit operator && to avoid a NPE. And honestly I didn't expect this nitpick was about this issue (as you are comparing an int variable and an int constant with the == operator).
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad. I stand corrected. Don't know what I was thinking.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!