• 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

JOptionPane OK_CANCEL_OPTION help

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the static method showInputDialog() does not throw such Exceptions.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad. I stand corrected. Don't know what I was thinking.
 
Their achilles heel is the noogie! Give them noogies tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic