swing - Java - how can I check what is exactly entered into a JOptionPane.showInputDialog -
this code
import java.util.random; import javax.swing.joptionpane; public class randomnumbersv2 { public static void main(string[] args){ double randomnumber = double.parsedouble(joptionpane.showinputdialog("please enter maximum number program generate")); random rnd = new random(); system.out.println(rnd.nextint(how make sure eneted in joption can put here)); // change int whatever number want, number max random number generated joptionpane.showmessagedialog(null, "your random number is" + randomnumber); } }
random#nextint accepts integer value upper bound. therefore value entered should such. also, per docs, number should positive:
try { int maxnumber = integer.parseint(joptionpane.showinputdialog("please enter input")); if (maxnumber > 0) { random rnd = new random(); system.out.println(rnd.nextint(maxnumber)); } else { throw new illegalargumentexception("non positive number not allowed"); } } catch (numberformatexception e) { throw new illegalargumentexception("invalid integer", e); }
Comments
Post a Comment