package start6_2; import javax.swing.JOptionPane; import java.util.Scanner; /** * This example illustrates how to reduce JOptionPane pain. */ public class JOptionPain { public static void main(String[] args) { // The old way: String input = null; double consumedKg = -1; String message = "Please enter the amount you ate at Christmas, in kg: "; while(input == null || consumedKg < 0) { input = JOptionPane.showInputDialog(message); if(input != null) { Scanner line = new Scanner(input); // Yes, this works! if(line.hasNextDouble()) { consumedKg = line.nextDouble(); } else { message = "Please enter the amount you ate at Christmas, in kg. Needs to be a double."; } if(consumedKg < 0) { message = "Please enter the amount you ate at Christmas, in kg. Needs to be positive."; } } else { message = "Please enter the amount you ate at Christmas, in kg. Escape not optional."; } } JOptionPane.showMessageDialog(null,"You ate "+consumedKg+" kg of food!?!??!"); } }