Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JavaFX I'm making a basic calculation program that takes the input from 2 textfields and adds them together. Usually, I use something along the lines

JavaFX

I'm making a basic calculation program that takes the input from 2 textfields and adds them together. Usually, I use something along the lines of Integer.parseInt(operand1.getText()) to turn the textfield into and integer. However, the program doesn't like it as I keep getting an error message: java.lang.NumberFormatException: For input string: "" where I use that to get an integer. I need my program to remain formatted with the same basic content as it is now, but can someone please tell me how I can get an integer from my string?

import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class BasicCalculator extends Application { @Override public void start(Stage primaryStage) { BorderPane pane = new BorderPane(); Button enter = new Button("Add"); TextField operand1 = new TextField(); TextField operand2 = new TextField(); Label plus = new Label("+"); Label answer = new Label(); primaryStage.setTitle("Basic Calculator"); VBox vbox = new VBox(); vbox.getChildren().addAll(operand1, operand2, plus, enter, answer); pane.setCenter(vbox); System.out.println(Integer.parseInt(operand1.getText())); enter.setOnAction( e -> { try{ int result = (Integer.parseInt(operand1.getText())) + (Integer.parseInt(operand2.getText())); answer.setText(Integer.toString(result)); } catch (NumberFormatException ex) { answer.setText("Error"); } }); Scene stScene = new Scene(pane, 1080, 720, Color.GREEN); primaryStage.setScene(stScene); primaryStage.show(); } public static void main (String[]args){ // write your code here launch(args); } }

Thanks

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

Students also viewed these Databases questions

Question

=+j Understand different types of regions in the world.

Answered: 1 week ago