【问题标题】:Parsing Double/Int from Jtextfield Input从 Jtextfield 输入解析 Double/Int
【发布时间】:2016-04-01 15:17:22
【问题描述】:

我是 Java 新手,我现在正在尝试创建一个程序,该程序使用 JTextfield 从用户接收值并使用该值执行一些计算。我遇到了JTextfield 将出现NumberFormatException 错误的问题 当我试图编译它时。这是我的代码:

import javax.swing.*;
import java.lang.*;
public class VehicleParking {
   public static void main(String args[]) {
      JTextField inh = new JTextField(2);
      JTextField inm = new JTextField(2);
      JTextField outh = new JTextField(2);
      JTextField outm = new JTextField(2);

      JPanel InPanel = new JPanel();
      InPanel.add(new JLabel("In Hours: "));
      InPanel.add(inh);
      String inhour = inh.getText();
      double inhourInput = Double.valueOf(inhour);
      InPanel.add(Box.createHorizontalStrut(15));
      InPanel.add(new JLabel("Minutes :"));
      InPanel.add(inm);
      String inminute = inm.getText();
      double inminuteInput = Double.valueOf(inminute);

      JPanel OutPanel = new JPanel();


      OutPanel.add(new JLabel("Out Hours: "));
      OutPanel.add(outh);
      String outhour = outh.getText();
      double outhourInput = Double.valueOf(outhour);
      OutPanel.add(Box.createHorizontalStrut(15));
      OutPanel.add(new JLabel("Minutes :"));
      OutPanel.add(outm);
      String outminute = outm.getText();
      double outminuteInput = Double.valueOf(outminute);

这是我尝试编译时得到的结果:

Exception in thread "main" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at java.lang.Double.valueOf(Double.java:502)
    at VehicleParking.main(VehicleParking.java:23)

底部还有更多用于显示和算术目的。 另外,我尝试使用 try 和 catch,但它根本没有解决我的问题。(出现了更多错误。)这是我使用 try 和 catch 尝试解决数字后的完整程序格式异常问题。(我知道这样简单的算术程序有点太长了。对不起。)

import javax.swing.*;
import java.lang.*;
public class VehicleParking {
   public static void main(String args[]) {
      JTextField inh = new JTextField(2);
      JTextField inm = new JTextField(2);
      JTextField outh = new JTextField(2);
      JTextField outm = new JTextField(2);

      JPanel InPanel = new JPanel();
      InPanel.add(new JLabel("In Hours: "));
      InPanel.add(inh);
      InPanel.add(Box.createHorizontalStrut(15));
      InPanel.add(new JLabel("Minutes :"));
      InPanel.add(inm);

      JPanel OutPanel = new JPanel();


      OutPanel.add(new JLabel("Out Hours: "));
      OutPanel.add(outh);
      OutPanel.add(Box.createHorizontalStrut(15));
      OutPanel.add(new JLabel("Minutes :"));
      OutPanel.add(outm);




      int choice = JOptionPane.showConfirmDialog(null, InPanel, "Vehicle Parking System",JOptionPane.OK_CANCEL_OPTION);
            if (choice == JOptionPane.OK_OPTION)
                {
                    try
                        {String inhour = inh.getText();
                        double inhourInput = Double.valueOf(inhour);}
                    catch (NumberFormatException e){
                        if (inhour == null || inhour.equals(""))
                            {inhourInput = 0.0;}
                        else
                            {inhourInput = 0.0;}}


                    try
                        {String inminute = inm.getText();
                        double inminuteInput = Double.valueOf(inminute);}
                    catch (NumberFormatException e){
                        if (inminute == null || inminute.equals(""))
                            {inminuteInput = 0.0;}
                        else
                            {inminuteInput = 0.0;}}

                    int choice2 = JOptionPane.showConfirmDialog(null, OutPanel, "Vehicle Parking System",JOptionPane.OK_CANCEL_OPTION);
                    if (choice2 == JOptionPane.OK_OPTION)
                        {
                            try
                                {String outhour = outh.getText();
                                double outhourInput = Double.valueOf(outhour);}
                            catch (NumberFormatException e){
                                if (outhour == null || outhour.equals(""))
                                    {outhourInput = 0.0;}
                                else
                                    {outhourInput = 0.0;}}

                            try
                                {String outminute = outm.getText();
                                double outminuteInput = Double.valueOf(outminute);}
                            catch (NumberFormatException e){
                                if (outminute == null || outminute.equals(""))
                                    {outminuteInput = 0.0;}
                                else
                                    {outminuteInput = 0.0;}}
                        double hour = outhourInput - inhourInput;
                        double minute = outminuteInput - inminuteInput;
                        String[] VehicleType = {"Car","Van","Bus","Lorry"};
                        String typeVehicle =(String)JOptionPane.showInputDialog (null, "Choose vehicle type: ","Vehicle Parking System",JOptionPane.PLAIN_MESSAGE,
                                                                                null,VehicleType,VehicleType[0]);
                        switch (typeVehicle)
                            {case "Car" :   if (minute <0)
                                                {minute = minute +60;
                                                hour = hour - 1;
                                                double calcalateDuration = hour + (minute/60);
                                                double fee = calcalateDuration;
                                                String show = "Parking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,show,"Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                JOptionPane.showMessageDialog(null,"Please take your receipt.","Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                String receipt =    "In time: "+inhourInput+" "+inminuteInput+
                                                                    "\nOut time: "+outhourInput+" "+inminuteInput+
                                                                    "\nDuration: "+hour+"hours and "+minute+"minutes."+
                                                                    "\nVehicle Type: "+typeVehicle+
                                                                    "\nParking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,receipt,"Vehicle Parking System - Parking Receipt",JOptionPane.INFORMATION_MESSAGE);}
                                            else
                                                {double calcalateDuration = hour + (minute/60);
                                                double fee = calcalateDuration;
                                                String show = "Parking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,show,"Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                JOptionPane.showMessageDialog(null,"Please take your receipt.","Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                String receipt =    "In time: "+inhourInput+" "+inminuteInput+
                                                                    "\nOut time: "+outhourInput+" "+inminuteInput+
                                                                    "\nDuration: "+hour+"hours and "+minute+"minutes."+
                                                                    "\nVehicle Type: "+typeVehicle+
                                                                    "\nParking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,receipt,"Vehicle Parking System - Parking Receipt",JOptionPane.INFORMATION_MESSAGE);}
                                            break;

                            case "Van"  :   if (minute <0)
                                                {minute = minute +60;
                                                hour = hour - 1;
                                                double calcalateDuration = hour + (minute/60);
                                                double fee = calcalateDuration*120/100;
                                                String show = "Parking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,show,"Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                JOptionPane.showMessageDialog(null,"Please take your receipt.","Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                String receipt =    "In time: "+inhourInput+" "+inminuteInput+
                                                                    "\nOut time: "+outhourInput+" "+inminuteInput+
                                                                    "\nDuration: "+hour+"hours and "+minute+"minutes."+
                                                                    "\nVehicle Type: "+typeVehicle+
                                                                    "\nParking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,receipt,"Vehicle Parking System - Parking Receipt",JOptionPane.INFORMATION_MESSAGE);}
                                            else
                                                {double calcalateDuration = hour + (minute/60);
                                                double fee = calcalateDuration*120/100;
                                                String show = "Parking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,show,"Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                JOptionPane.showMessageDialog(null,"Please take your receipt.","Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                String receipt =    "In time: "+inhourInput+" "+inminuteInput+
                                                                    "\nOut time: "+outhourInput+" "+inminuteInput+
                                                                    "\nDuration: "+hour+"hours and "+minute+"minutes."+
                                                                    "\nVehicle Type: "+typeVehicle+
                                                                    "\nParking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,receipt,"Vehicle Parking System - Parking Receipt",JOptionPane.INFORMATION_MESSAGE);}
                                            break;
                            case "Bus"  :   if (minute <0)
                                                {minute = minute +60;
                                                hour = hour - 1;
                                                double calcalateDuration = hour + (minute/60);
                                                double fee = calcalateDuration*140/100;
                                                String show = "Parking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,show,"Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                JOptionPane.showMessageDialog(null,"Please take your receipt.","Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                String receipt =    "In time: "+inhourInput+" "+inminuteInput+
                                                                    "\nOut time: "+outhourInput+" "+inminuteInput+
                                                                    "\nDuration: "+hour+"hours and "+minute+"minutes."+
                                                                    "\nVehicle Type: "+typeVehicle+
                                                                    "\nParking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,receipt,"Vehicle Parking System - Parking Receipt",JOptionPane.INFORMATION_MESSAGE);}
                                            else
                                                {double calcalateDuration = hour + (minute/60);
                                                double fee = calcalateDuration*140/100;
                                                String show = "Parking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,show,"Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                JOptionPane.showMessageDialog(null,"Please take your receipt.","Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                String receipt =    "In time: "+inhourInput+" "+inminuteInput+
                                                                    "\nOut time: "+outhourInput+" "+inminuteInput+
                                                                    "\nDuration: "+hour+"hours and "+minute+"minutes."+
                                                                    "\nVehicle Type: "+typeVehicle+
                                                                    "\nParking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,receipt,"Vehicle Parking System - Parking Receipt",JOptionPane.INFORMATION_MESSAGE);}
                                            break;

                            case "Lorry" :  if (minute <0)
                                                {minute = minute +60;
                                                hour = hour - 1;
                                                double calcalateDuration = hour + (minute/60);
                                                double fee = calcalateDuration*160/100;
                                                String show = "Parking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,show,"Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                JOptionPane.showMessageDialog(null,"Please take your receipt.","Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                String receipt =    "In time: "+inhourInput+" "+inminuteInput+
                                                                    "\nOut time: "+outhourInput+" "+inminuteInput+
                                                                    "\nDuration: "+hour+"hours and "+minute+"minutes."+
                                                                    "\nVehicle Type: "+typeVehicle+
                                                                    "\nParking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,receipt,"Vehicle Parking System - Parking Receipt",JOptionPane.INFORMATION_MESSAGE);}
                                            else
                                                {double calcalateDuration = hour + (minute/60);
                                                double fee = calcalateDuration*160/100;
                                                String show = "Parking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,show,"Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                JOptionPane.showMessageDialog(null,"Please take your receipt.","Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);
                                                String receipt =    "In time: "+inhourInput+" "+inminuteInput+
                                                                    "\nOut time: "+outhourInput+" "+inminuteInput+
                                                                    "\nDuration: "+hour+"hours and "+minute+"minutes."+
                                                                    "\nVehicle Type: "+typeVehicle+
                                                                    "\nParking fee: "+fee;
                                                JOptionPane.showMessageDialog(null,receipt,"Vehicle Parking System - Parking Receipt",JOptionPane.INFORMATION_MESSAGE);}
                                            break;
                            default     :   JOptionPane.showMessageDialog(null,"Unknown Error Occurred!","Vehicle Parking System",JOptionPane.ERROR_MESSAGE);
                                            break;}}


                    else
                        JOptionPane.showMessageDialog(null,"Process Canceled.","Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);}
            else
                JOptionPane.showMessageDialog(null,"Process Canceled.","Vehicle Parking System",JOptionPane.INFORMATION_MESSAGE);



   }
}

我已经被困在这里半天了,对某些人来说可能还不够长。但我确实需要一些帮助。谢谢。

【问题讨论】:

  • 只是为了清楚;您在编译时收到错误消息?在第几行?
  • 我已经编辑了帖子,结果显示我在不使用 try 和 catch 方式进行编译时收到了异常错误。
  • 好的,使用 try/catch 方法的较低示例是一个更好的示例如果您解决了变量的范围问题。您应该始终尝试捕获数字异常。

标签: java parsing try-catch jtextfield numberformatexception


【解决方案1】:

在第一个示例中,导致问题的行似乎是:

double outhourInput = Double.valueOf(outhour);

引用的异常跟踪表明 outhour 为空/null。因此,在转换之前,请检查 outhour 是否为空(例如,outhour != null &amp;&amp; ! outhour.trim().isEmpty())。

将代码(来自第二个示例)复制到 IDE 后,很快就可以清楚问题出在哪里。您没有对 NumberFormatException 本身有问题。这个问题存在于多个地方,是变量的范围问题。

       if (choice == JOptionPane.OK_OPTION) {
        try {
            String inhour = inh.getText();  //-->inhour defined here
            double inhourInput = Double.valueOf(inhour);
        }
        catch (NumberFormatException e) {
            if (inhour == null || inhour.equals("")) {  //-->no scope for inhour
                inhourInput = 0.0;
            }
            else {
                inhourInput = 0.0;
            }
        }

在所有块中,您必须确保定义的变量在 catch 块的范围内。但是,我看不到 if 测试的意义,因为无论如何它们都将变量设置为 0.0。

尝试以下方法:

double inhourInput = 0.0;
String inhour = inh.getText();
try {
   inhour = inh.getText();
   inhourInput = Double.valueOf(inhour);
}
catch (NumberFormatException e) {
  // do not really need to reset to 0, but can be useful for clarity
  inhoutInput = 0.0;
}

【讨论】:

  • 对我来说很棒的解决方案。它完全解决了我的问题。非常感谢!虽然我收到一个关于“inhour”的小错误,但已经宣布了两次。但它可以很容易地解决。再次感谢!
  • @LeonSeveron,你是对的,我在移动东西时未能删除try { 中的字符串声明。进行了编辑。很高兴它有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多