0
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);

I want the employeeInput to be filled and saved, untill it is replaced by another employeeInput. The problem I'm having is when getting the item input, the employeeInput is now missing. What is the way to do this?

Thank you, for your help

3 Answers 3

1

employeeInput is a method variable, so everytime you exit the method you will lose the reference to it.

The obvious thing to try is turn employeeInput into a member variable. Just declare it at the top of your class.

Better yet may be to persist that value to a database.

Sign up to request clarification or add additional context in comments.

1 Comment

Dear lord I'm border line mentally challenged I swear. Thank you
1

The compiler doesn't know that the first condition has already been met. Nor, do I. But, you could change

String employeeInput; // <-- may not have been initialized

to a default value (possibly even explicitly null), here the empty String "".

String employeeInput = "";

1 Comment

Thanks I had set it to null. string employeeInput = null; but when I call upon it in the elseif part it would put the employeeinput back to null instead of the previous scan. :( I'm hopeless. Is there a way to make the employeeInput stay untill it has been inputed a second time? instead of whipped back to null or "" each time?
0

It doesn't look like your employeeInput variable is 'missing' or could be removed after any other variable is set. So its most likely you aren't initializing your variables when they're defined.

Try initializing them like

String employeeInput = ""; // or = null
String assetInput = ""; // or = null
String userInput = txtUserInput.getText().toLowerCase();

And your problem should be solved. The compiler might also be giving you a warning about this.

You might also want to try removing this txtUserInput.setText(""); line to test for problems.

Edit:

If you are trying to access this variable after your method has finished executing then it will be wiped so instead of putting it in your method declare it in your class like public class Name{ public /*static*/ String employeeInput = ""; //or null }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.