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

help with JOptionpane

 
Adam C.
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how would i get JOptionpane to change an int variable when a number is input?



how would i get those two to communicate?
trying number= gives me a required int error

[ August 31, 2005: Message edited by: Adam C. ]
[ August 31, 2005: Message edited by: Adam C. ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int anInt = Integer.parseInt(guess);

(but you will need to combine it with some error-handling)
 
Adam C.
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


something like that, or am i way off?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
showInputDialog(..) returns a String
Integer.parseInt(String arg) takes a String as an argument and returns an int

try re-working your posted code
(taking a closer look at your original and my original posts)
 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May as well warn you now, your name doesn't fit the naming policy. In other words you need to have a full last name, not an inital!

And on your code, the whole putting an int to an optionPane just doesn't seem right to me, but I'm not very experenced and it may be perfectly fine. Sorry I'm not of much help there!
[ August 31, 2005: Message edited by: Atrus Greyor ]
 
Adam C.
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by int i mean a number input by a person
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would you like any further responses posted here, or at sun?

http://forum.java.sun.com/thread.jspa?threadID=658834
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi adam,
These two links will take you to the two packages that contain the classes that you need to consider Class JOptionPane with showinputDialog method that returns a String , even though you are typing in a number at JOptionPane it is still recognised as a string ,it must be parsed .Thejava lang package (which is imported implicitly) which contains the class, which contains the methods for changing your String input into an int

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/package-summary.html
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/package-summary.html
[ August 31, 2005: Message edited by: Daniel .J.Hyslop ]
 
Adam C.
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all for your help, i finally got it completely working


almost the whole time i was just missing the number=(int) typecast
[ August 31, 2005: Message edited by: Adam C. ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adam C.:
almost the whole time i was just missing the number=(int) typecast


Actually the cast shouldn't be necessary, as parseInt already returns an int, doesn't it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic