私はJavaで新しく、JTextfield
を使用してユーザーから値を受け取るプログラムを作成しようとしています。コンパイルしようとすると、JTextfield
にNumberFormatException
エラー があるという問題が発生しました。ここに私のコードは次のとおりです。Jtextfield入力からダブル/ Intを解析する
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を使用しようとしましたが、問題はまったく解決しません(さらにエラーが表示されました)。数値書式の例外の問題。(私はそれが何らかの形でこのような単純な算術演算プログラムには長すぎることを知っている。申し訳ありません。)
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);
}
}
私は何人かの人々のためにそうでないかもしれない十分な長さ、半日ここでstuckedてきました。しかし、私は本当に助けが必要です。ありがとう。最初の例で
;:
することなどのアプローチを試してみてください_compile_を実行するとエラーが発生していますか?どの行番号? – KevinO
私はtryとcatchの方法を使用せずにコンパイル中に例外エラーを受け取ったという追加の結果を表示して投稿を編集しました。 – LeonSeveron
OK、try/catchアプローチを使用した下の例は、変数を使ってスコープの問題を修正した方が良いでしょう。あなたは常に番号例外をキャッチしようとする必要があります。 – KevinO