如果用户输入的字符不是整数,则会显示错误消息

时间:2016-04-22 07:37:44

标签: java javafx

我正在为我所在的课程编写此程序,并且我已经能够创建此代码。我知道这很乱,我道歉。我需要找到一种方法让程序阻止用户输入除整数之外的任何内容。如果我有办法让它检查我将能够让自己的新舞台出现。如果有任何想法,我会很高兴听到他们。我再也不是要求任何人为我制作应用程序我只需要一种方法来检查是否存在除整数之外的字符并在这种情况下执行某些操作。像一个for else循环。提前致谢。 (这是我第一次使用JavaFX,所以请轻松一下:/

import java.util.InputMismatchException;

import org.omg.CORBA.portable.InputStream;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.event.*;
public class Lab9Test extends Application  {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

//launch it all
launch(args);
    }
 @Override //create the grids scenes stages needed to run the program
    public void start(Stage primaryStage) throws Exception {
        // TODO Auto-generated method stub
     final Stage dialogStage = new Stage();
     Label Label1 = new Label ("ERROR! You cannot divide by zero!");
    Button Okay = new Button("Okay!"); 
    GridPane GPane2 = new GridPane();
     GPane2.setAlignment(Pos.TOP_LEFT);
     GPane2.setVgap(10);
        GPane2.setHgap(-32);
        GPane2.setPadding(new Insets(10,10,10,10));
        Scene scene2 = new Scene(GPane2, 222,300);
        GridPane.setConstraints(Okay,4,1);
        GridPane.setConstraints(Label1,0,2);


    VBox root = new VBox();

    dialogStage.setScene(scene2);

     dialogStage.getIcons().add(new 

     GPane2.getChildren().addAll(Label1, Okay);
     Okay.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                dialogStage.close();    

            }
        });


     primaryStage.setTitle("JavaFX Arithmatic");

        GridPane GPane = new GridPane();

        GPane.setAlignment(Pos.TOP_LEFT);
        GPane.setVgap(10);
        GPane.setHgap(-75);
        GPane.setPadding(new Insets(10,10,10,10));
        Scene scene = new Scene(GPane, 325, 250);


        Button add = new Button();
        add.setText("+");
        Button multiply = new Button();
        multiply.setText("*");
        Button divide = new Button();
        divide.setText("/");
        Button subtract = new Button();
        subtract.setText("-");
        Button percent = new Button();
        percent.setText("%");
        Button clear = new Button();
        clear.setText("CLEAR");



        Text scenetitle = new Text("Enter two numbers and select an operation.");
        Text SecondNumber = new Text("Second Number:");
        Text FirstNumber = new Text("First Number:");
        Text Result = new Text("Result:");
        //create text fields
        final TextField TFFirstNumber = new TextField();
        final TextField TFSecondNumber = new TextField();
        final TextField TFResult = new TextField();

        //change position on grid
        GridPane.setConstraints(FirstNumber,0,1);
        GridPane.setConstraints(SecondNumber,0,2);
        GridPane.setConstraints(Result,0,3);
        GridPane.setConstraints(add,0,4);
        GridPane.setConstraints(multiply,0,5);
        GridPane.setConstraints(percent,0,6);
        GridPane.setConstraints(divide,1,5);
        GridPane.setConstraints(subtract,1,4);
        GridPane.setConstraints(clear,1,6);
        GridPane.setConstraints(scenetitle,0,0);
        GridPane.setConstraints(TFFirstNumber,1,1);
        GridPane.setConstraints(TFSecondNumber,1,2);
        GridPane.setConstraints(TFResult,1,3);

        GPane.getChildren().addAll(TFResult,TFFirstNumber,TFSecondNumber,add,FirstNumber,SecondNumber,Result,subtract,multiply,divide,percent,clear,scenetitle);
        //change size and alter the text fields 
        add.setMaxWidth(150);
        subtract.setMaxWidth(150);
        clear.setMaxWidth(150);
        divide.setMaxWidth(150);
        percent.setMaxWidth(150);
        multiply.setMaxWidth(150);
        TFFirstNumber.setMaxWidth(150);
        TFSecondNumber.setMaxWidth(150);
        TFResult.setMaxWidth(150);
        TFResult.setEditable(false);
        primaryStage.setScene(scene);
        primaryStage.getIcons().add(new 
        primaryStage.show();
        //give all of the buttons a purpose in life
        add.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){

            Integer value1  = Integer.valueOf(TFFirstNumber.getText());
            Integer value2  = Integer.valueOf(TFSecondNumber.getText());
            Integer r = value1 + value2;
            TFResult.setText(r.toString());
            }
        });
        subtract.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){

                Integer value1  = Integer.valueOf(TFFirstNumber.getText());
                Integer value2  = Integer.valueOf(TFSecondNumber.getText());
                Integer r = value1 - value2;
                TFResult.setText(r.toString());
            }
        });
        multiply.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                Integer value1  = Integer.valueOf(TFFirstNumber.getText());
                Integer value2  = Integer.valueOf(TFSecondNumber.getText());
                Integer r = value1 * value2;
                TFResult.setText(r.toString());
            }
        });
        divide.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                Integer value1  = Integer.valueOf(TFFirstNumber.getText());
                Integer value2  = Integer.valueOf(TFSecondNumber.getText());

                if(value2==0){
                    dialogStage.show();
                }
                else{
                    Integer r = value1 / value2;
                    TFResult.setText(r.toString());
                }
            }
        });
        percent.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                Integer value1  = Integer.valueOf(TFFirstNumber.getText());
                Integer value2  = Integer.valueOf(TFSecondNumber.getText());
                Integer r = value1 % value2;
                TFResult.setText(r.toString());
            }
        });
        clear.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
            TFFirstNumber.clear();
            TFSecondNumber.clear();
            TFResult.clear();
            }
        });
 }





    }

1 个答案:

答案 0 :(得分:0)

副本提供了一个很好的解决方案,但还有另一种方法:

你可以用这样的方式解析整数:

public void handle(ActionEvent event) {
    Integer value1 = 0;
    Integer value2 = 0;
    try {
        value1  = Integer.valueOf(TFFirstNumber.getText());
        value2  = Integer.valueOf(TFSecondNumber.getText());
    } catch(Exception e) {
        System.err.println("Wrong input value: needs to be an integer");
        return; // i.e. do mothing more
    }
    Integer r = value1 + value2;
    TFResult.setText(r.toString());
}