Results 1 to 2 of 2
- 02-02-2012, 10:42 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 1
- Rep Power
- 0
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; } }
- 02-03-2012, 05:43 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: Help fixing a malfunctioning GUI code.
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 accordinglyHowever, when the user enters an invalid integer and the GUI throws the error message
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
Similar Threads
-
Another problem that needs fixing : )
By Mirr0ge in forum New To JavaReplies: 3Last Post: 03-28-2011, 10:23 PM -
Fixing my if statement
By coding in forum New To JavaReplies: 11Last Post: 03-01-2011, 01:56 AM -
Help with fixing timer
By acash229 in forum New To JavaReplies: 1Last Post: 09-13-2010, 06:27 AM -
Undo shortcut malfunctioning.
By Eranga in forum NetBeansReplies: 0Last Post: 03-31-2008, 05:12 AM -
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03-19-2008, 10:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks