如何从同一文本字段中获取多个整数?

时间:2017-02-23 13:04:19

标签: javafx javafx-2 javafx-8

我想从同一文本字段中检索几个整数,但我无法弄清楚如何做到这一点。任何人吗?

1 个答案:

答案 0 :(得分:1)

这是一个更清晰的解决方案。对于错误输入,这将抛出错误。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication37 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label info = new Label("Enter some numbers. Each seperated by one space");

        TextField tf = new TextField();
        Label label = new Label();

        Button btn = new Button();
        btn.setText("Sum Numbers");
        btn.setOnAction(new EventHandler<ActionEvent>() {            
            @Override
            public void handle(ActionEvent event) {
                if(tf.getText().length() > 0)//Only run if the text field is not empty
                {
                    String[] stringOfNumbers = tf.getText().split(" ");//The numbers must be seperated by one space. This get the string of numbers and seperates them based on the space between each numbers

                    //sum up the numbers
                    double sum = 0;
                    for(int i = 0; i < stringOfNumbers.length; i++)
                    {
                        sum = sum + Double.parseDouble(stringOfNumbers[i]);//This sums the numbers and convert the strings to doubles                  
                    }
                    label.setText("The sum equals: " + sum);//This prints the sum of the doubles   
                }
            }
        });

        VBox root = new VBox();
        root.getChildren().addAll(info, tf, label, btn);//This adds all the nodes to the VBox

        Scene scene = new Scene(root, 300, 250);//This adds the VBox to the Scene

        primaryStage.setTitle("Sum Numbers");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }    
}
  

我注意到这个问题需要处理整数。在此代码中,将double更改为int,将Double更改为Integer。

我正在添加此解决方案,因为您的解决方案不包含按钮

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication37 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label info = new Label("Enter some numbers each seperated by one space");
        Label label = new Label();

        TextField tf = new TextField();
        tf.textProperty().addListener((observable, oldValue, newValue) -> {
            try
            {
                if(newValue.length() > 0)//Only run if the text field is not empty
                {
                    String[] stringOfNumbers = tf.getText().split(" ");//The numbers must be seperated by one space. This get the string of numbers and seperates them based on the space between each numbers

                    //sum up the numbers
                    int sum = 0;
                    for(int i = 0; i < stringOfNumbers.length; i++)
                    {
                        sum = sum + Integer.parseInt(stringOfNumbers[i]);//This sums the numbers and convert the strings to doubles                  
                    }
                    label.setText("The sum equals: " + sum);//This prints the sum of the doubles   
                }    
                else
                {
                    label.setText("");//if no text in textfield set label to empty string
                }
            }
            catch(NumberFormatException ex)
            {
                label.setText("");//any number format error set label to empty string
            }
        });     

        VBox root = new VBox();
        root.getChildren().addAll(info, tf, label);//This adds all the nodes to the VBox

        Scene scene = new Scene(root, 300, 250);//This adds the VBox to the Scene

        primaryStage.setTitle("Sum Numbers");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }    
}