I have 95% of my code written but cannot figure out how to set up the error checking for when the user enters letters in a field that requires numbers.
Assignment : Create an interactive gui program that converts miles to kilometers and vice versa, dependent on what the user chooses. Also requires error checking for input other than integers or doubles.
Here is my code thus far.
[highlight = java][/highlight]import javax.swing.JOptionPane; public class Converter { public static void main(String args[]) { String converterChoice; int i = 0; // counter for loop while (i < 1) // loop continues until the user enters valid information { converterChoice = JOptionPane.showInputDialog("Would you like to convert English or Metric distance: "); if (converterChoice.equals("Metric")) metricCoverter(); else if (converterChoice.equals("English")) englishConverter(); else JOptionPane.showMessageDialog(null, "Your Entry is invalid, please retry", "Error", JOptionPane.INFORMATION_MESSAGE); } } public static void metricCoverter() { String metricEntry; double metricDouble; double conversion; metricEntry = JOptionPane.showInputDialog("Enter the number of kilometers to be converted to miles: "); //create a double from the string metricDouble = Double.parseDouble(metricEntry); //convert to kilometers conversion = metricDouble * .62; JOptionPane.showMessageDialog(null, "Your entry of " + metricEntry + "miles converts to " + conversion + "kilomters", "Conversion to Kilometers", JOptionPane.INFORMATION_MESSAGE); } public static void englishConverter() { String englishEntry; double englishDouble; double conversion; englishEntry = JOptionPane.showInputDialog("Enter the number of miles to be converted to kilometers: "); //create a double from the string englishDouble = Double.parseDouble(englishEntry); //convert to miles conversion = englishDouble * 1.609344; JOptionPane.showMessageDialog(null, "Your entry of " + englishEntry + "kilometers converts to " + conversion + "miles", "Conversion to Miles", JOptionPane.INFORMATION_MESSAGE); } }
Can anyone give me a hint on this one?
Thanks