Results 1 to 2 of 2
  1. #1
    befigard is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default Help fixing a malfunctioning GUI code.

    My GUI code is here, it works as planned for the most part. However, when the user enters an invalid integer and the GUI throws the error message, I need the GUI to pull the message back up where the user is to re-enter the corrected integer. I am NEW to java, and very stuck on this. Can someone please help me.

    Java Code:
    
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
                WeightCalculator wc = new WeightCalculator();
                boolean validData = false;
                double weight = 0;
                String strWeight = "";
                String strDescription = "";
                Scanner s = new Scanner(System.in);
    
                do {
                    strDescription = JOptionPane.showInputDialog("Enter description of the object ");//input from user
                    strWeight = JOptionPane.showInputDialog("Enter weight of the object ");//input from user
    
    
                    weight = Double.parseDouble(strWeight);
    
                    while (weight < 0 == weight < 99999) {//sets the parimeters for the error message
                        validData = true;
    
    
                        JOptionPane.showMessageDialog(null, "Please enter valid value for weight. It should be between 0 and 99999","Inane Error", JOptionPane.ERROR_MESSAGE);//output to user, error message     
                        validData = false;
    
                                   
    
                    }
    
                } while (validData = false);
    
                wc.setWeight(weight);
                wc.setDescription(strDescription);
    
                wc.setWeight(weight);
                wc.setDescription(strDescription);
    
                JOptionPane.showMessageDialog(null, "The weight of on Mercury is: " + wc.mercuryWeight(), "Result ", JOptionPane.PLAIN_MESSAGE);
                JOptionPane.showMessageDialog(null, "The weight of on Earth is: " + wc.earthWeight(), "Result ", JOptionPane.PLAIN_MESSAGE);
                JOptionPane.showMessageDialog(null, "The weight of on the Moon is: " + wc.moonWeight(), "Result ", JOptionPane.PLAIN_MESSAGE);
                JOptionPane.showMessageDialog(null, "The weight of  on Jupiter is: " + wc.jupiterWeight(), "Result ", JOptionPane.PLAIN_MESSAGE);
    
    
    
            }
        }
    
    
    /** 
     * 
     * @author Bert 
     */
    class WeightCalculator {
    
        private double weight = 0.0;
        private String description = null;
    
        public double earthWeight() {
            return weight;
        }
    
        public double mercuryWeight() {
            return weight * .378;
        }
    
        public double moonWeight() {
            return weight * .166;
        }
    
        public double jupiterWeight() {
            return weight * 2.364;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setWeight(double weight) {
            this.weight = weight;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Help fixing a malfunctioning GUI code.

    However, when the user enters an invalid integer and the GUI throws the error message
    What error message? You need to post if you don't want us to guess. My guess: its a NumberFormatException - use a try/catch and deal with the exception accordingly
    Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

Similar Threads

  1. Another problem that needs fixing : )
    By Mirr0ge in forum New To Java
    Replies: 3
    Last Post: 03-28-2011, 10:23 PM
  2. Fixing my if statement
    By coding in forum New To Java
    Replies: 11
    Last Post: 03-01-2011, 01:56 AM
  3. Help with fixing timer
    By acash229 in forum New To Java
    Replies: 1
    Last Post: 09-13-2010, 06:27 AM
  4. Undo shortcut malfunctioning.
    By Eranga in forum NetBeans
    Replies: 0
    Last Post: 03-31-2008, 05:12 AM
  5. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •