Java 如何检查对话框是否为空?
我正在写一个程序,我开始写这个部分(我已经在下面评论过了),我试着去做Java 如何检查对话框是否为空?,java,if-statement,javafx,null,Java,If Statement,Javafx,Null,我正在写一个程序,我开始写这个部分(我已经在下面评论过了),我试着去做 if(userInput.get() != "") 如果我将对话框留空并按“ok”或enter键,它仍然会执行if语句中的代码。所以它会说: "You did not enter numeric information" 而不是 "You did not enter any information" 就像外部else语句一样 我尝试了null,但它也不起作用。我做错了什么 import java.util.Optio
if(userInput.get() != "")
如果我将对话框留空并按“ok”或enter键,它仍然会执行if语句中的代码。所以它会说:
"You did not enter numeric information"
而不是
"You did not enter any information"
就像外部else语句一样
我尝试了null,但它也不起作用。我做错了什么
import java.util.Optional;
import javafx.application.*;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.text.*;
import javafx.stage.*;
import javafx.stage.Stage;
public class QuizDialogBox extends Application {
public void start(Stage primaryStage) {
TextInputDialog inputDialog = new TextInputDialog();
inputDialog.setHeaderText(null);
inputDialog.setTitle("Grams to Ounces");
inputDialog.setContentText("Enter number of Grams: ");
Alert errorAlert = new Alert(AlertType.ERROR);
Optional<String> userInput = inputDialog.showAndWait();
if((userInput.get() != "") && userInput.isPresent()) //the part thats not working
{
String userInputString = userInput.get();
System.out.println("{" + userInputString + "}");
if(isNumeric(userInputString))
{
int userNumber = Integer.parseInt(userInputString);
System.out.println(userNumber);
}
else
{
errorAlert.setContentText("You did not enter numeric information");
errorAlert.showAndWait();
}
}
else
{
errorAlert.setContentText("You did not enter any information");
errorAlert.showAndWait();
}
}
public static void main(String args[]) {
launch(args);
}
public static boolean isNumeric(String string) {
boolean isNumber = false;
try
{
Integer.parseInt(string);
isNumber = true;
}
catch(NumberFormatException ex) {
}
return isNumber;
}
}
import java.util.Optional;
导入javafx.application.*;
导入javafx.event.*;
导入javafx.geometry.Pos;
导入javafx.scene.*;
导入javafx.scene.control.*;
导入javafx.scene.control.Alert.AlertType;
导入javafx.scene.input.*;
导入javafx.scene.layout.*;
导入javafx.scene.paint.*;
导入javafx.scene.shape.*;
导入javafx.scene.text.*;
导入javafx.stage.*;
导入javafx.stage.stage;
公共类QuizDialogBox扩展了应用程序{
公共无效开始(阶段primaryStage){
TextInputDialog inputDialog=新建TextInputDialog();
inputDialog.setHeaderText(空);
inputDialog.setTitle(“克到盎司”);
setContentText(“输入克数:”);
Alert errorAlert=新警报(AlertType.ERROR);
可选的userInput=inputDialog.showAndWait();
if((userInput.get()!=“”)和&userInput.isPresent())//不工作的部件
{
字符串userInputString=userInput.get();
System.out.println(“{”+userInputString+“}”);
if(isNumeric(userInputString))
{
int userNumber=Integer.parseInt(userInputString);
System.out.println(用户编号);
}
其他的
{
errorAlert.setContentText(“您没有输入数字信息”);
errorAlert.showAndWait();
}
}
其他的
{
errorAlert.setContentText(“您没有输入任何信息”);
errorAlert.showAndWait();
}
}
公共静态void main(字符串参数[]){
发射(args);
}
公共静态布尔值isNumeric(字符串){
布尔值isNumber=false;
尝试
{
整数.parseInt(字符串);
isNumber=true;
}
捕获(NumberFormatException ex){
}
返回isNumber;
}
}
将if(userInput.get()!=“”)修改为if(!userInput.get().isEmpty())
=
仅比较参考
或者您可以使用if(!userInput.get().equals(“”)
来比较两个字符串
如果userInput.get()
是null的
请小心检查null
,用替换(userInput.get()!=”)
!(“”.equals(userInput.get())
。安全检查。