###前提・実現したいこと
Javafxを使って2つのtextfieldに入力した値をChoiseBoxで+ー×÷を選択して、その通りに計算した結果を新しいウィンドウで計算結果として出すプログラム
###発生している問題・エラーメッセージ
最初は計算結果が0となって仕様どおりに動かなかったのですが、色々変えていったらランタイムエラーでプログラムが実行しなくなりました。
エラーメッセージ
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at Kadai14_1.keisan(Kadai14_1.java:50)
at Kadai14_1.start(Kadai14_1.java:33)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Exception running application Kadai14_1
該当のソースコード
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Kadai14_1 extends Application {
TextField textField1;
TextField textField2;
ChoiceBox<String> c;
Button button;
Alert alert;
static int result;
String a;
public void start(Stage stage) {
stage.setWidth(450);
stage.setHeight(200);
stage.setTitle("簡易計算機");
textField1 = new TextField(); textField2 = new TextField(); c = new ChoiceBox<>(); c.getItems().addAll("+","ー","×","÷"); c.setValue("+"); button = new Button("計算"); button.setOnAction(event -> alert.showAndWait()); alert = new Alert(AlertType.INFORMATION); alert.setTitle("計算結果"); alert.setContentText("答えは:" + keisan()); AnchorPane ap = new AnchorPane(); ap.getChildren().addAll(textField1,textField2,c,button); AnchorPane.setTopAnchor(textField1,30.0); AnchorPane.setLeftAnchor(textField1, 30.0); AnchorPane.setTopAnchor(c, 30.0); AnchorPane.setLeftAnchor(c, 192.0); AnchorPane.setTopAnchor(textField2,30.0); AnchorPane.setLeftAnchor(textField2, 250.0); AnchorPane.setTopAnchor(button,120.0); AnchorPane.setLeftAnchor(button, 200.0); stage.setScene(new Scene(ap)); stage.show(); } String keisan(){ int t1 = Integer.parseInt(textField1.getText()); int t2 = Integer.parseInt(textField2.getText()); if(c.getValue().equals("+")) { result = t1 + t2; } else if(c.getValue().equals("ー")) { result = t1 - t2; } else if(c.getValue().equals("×")) { result = t1 * t2; } else if(c.getValue().equals("÷")) { if(t2 == 0){ alert.showAndWait(); } result = t1 / t2; } a = String.valueOf(result); return a; } public static void main(String[] args) { launch(); }
}
###試したこと
最初はTextFieldに入力された値(intと想定して)計算させていましたが、keisanメソッドでストリングに変換してそれをリターンで返した値を、答えは:keisan()として出力させようとしました。
###補足情報(言語/FW/ツール等のバージョンなど)
java, javafx, eclipse4.6
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/04 06:37
2017/12/04 06:39
2017/12/04 06:44
2017/12/04 06:45