Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA IF-Else变量_Java - Fatal编程技术网

JAVA IF-Else变量

JAVA IF-Else变量,java,Java,我想填写并保存employeeInput,直到它被另一个employeeInput替换。我遇到的问题是,当获取项目输入时,employeeInput现在丢失了。这样做的方法是什么 感谢您的帮助编译器不知道第一个条件已经满足。我也不知道。但是,你可以改变 public void actionPerformed(ActionEvent arg0) { String employeeInput; String assetInput; String userInput = tx

我想填写并保存employeeInput,直到它被另一个employeeInput替换。我遇到的问题是,当获取项目输入时,employeeInput现在丢失了。这样做的方法是什么


感谢您的帮助

编译器不知道第一个条件已经满足。我也不知道。但是,你可以改变

public void actionPerformed(ActionEvent arg0) {
    String employeeInput;
    String assetInput;

    String userInput = txtUserInput.getText();
    userInput = userInput.toLowerCase();

    if (userInput.startsWith("emp")){

        //String employeeInput = null;
        employeeInput = userInput.replaceAll("\\s","");
        txtUserInput.setText("");

        JOptionPane.showMessageDialog(frame, "The Scan was " );

    }else if (userInput.startsWith("u")){

        assetInput = userInput;
        assetInput.replaceAll("\\s","");
        txtUserInput.setText("");


        System.out.println("Employee ID is " + **employeeInput**); //asks for employeeInput to be declared.

        JOptionPane.showMessageDialog(frame, "The Scan was " + assetInput);

employeeInput
是一个方法变量,因此每次退出该方法时,都会丢失对该方法的引用

显然,要尝试的是将
employeeInput
转换为成员变量。只要在你班的第一名宣布就行了


更好的方法是将该值持久化到数据库。

看起来您的
employeeInput
变量“丢失”或在设置任何其他变量后可以删除。因此,在定义变量时,很可能没有初始化变量

试着像这样初始化它们

String employeeInput = "";
你的问题应该得到解决。编译器可能还会就此向您发出警告

您可能还想尝试删除此
txtserinput.setText(“”)测试问题的行

编辑:

如果您试图在方法完成执行后访问此变量,那么它将被删除,因此,不要将其放在方法中,而是在类中声明它,如
public class Name{
public/*static*/String employeeInput=“”;//或null

}

谢谢,我已将其设置为空。字符串employeeInput=null;但当我在elseif部分调用它时,它会将employeeinput恢复为null,而不是以前的扫描:(我没希望了。有没有办法让员工输入信息一直保留到第二次输入?而不是每次都返回空值或“”?亲爱的上帝,我是边境线智障者,我发誓。谢谢你
String employeeInput = "";
String employeeInput = ""; // or = null
String assetInput = ""; // or = null
String userInput = txtUserInput.getText().toLowerCase();