1

I want to get user1.name variable from public static void FileRead()to private void jButton1ActionPerformed but It doesn't read user1.name. Can you explain me how can I use that variable.

public static class Users {
    public String name;
    public String password;

    Users(String name1, String password1) {
        name = name1;
        password = password1;
    }

}

public static void FileRead() {
    try {
        BufferedReader in = new BufferedReader(new FileReader("C:/Users/B_Ali/Documents/NetBeansProjects/JavaApplication20/UserNamePassword.txt"));
        String[] s1 = new String[5];
        String[] s2 = new String[5];
        int i = 0;
        while ((s1[i] = in.readLine()) != null) {
            s1[i] = s2[i];
            i = i + 1;

            if (i == 1) {
                Users user1 = new Users(s2[0], s2[1]);
            }
            else if (i == 3) {
                Users user2 = new Users(s2[2], s2[3]);
            }
            else if (i == 5) {
                Users user3 = new Users(s2[4], s2[5]);
            }
        }
        in.close();
    }
    catch (FileNotFoundException ex) {
        Logger.getLogger(LoginScreen.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
        Logger.getLogger(LoginScreen.class.getName()).log(Level.SEVERE, null, ex);
    }

}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    JOptionPane.showMessageDialog(null, user1.name);
    // TODO add your handling code here:
}

where should I declare it? Edit: if i declare it as you said before. It becomes meaningless because i want to use user1.name which is defined in FileRead().

2 Answers 2

2

Declare it global.

Users user1;

public static class Users {
    public String name;
    public String password;

    Users(String name1, String password1) {
        name = name1;
        password = password1;
    }
    public String getName()
    { return this.name;
       }
    public String getPassword()
    {return this.password;
     }
}

Users user1, user2;
public static void FileRead() {
    try {
        BufferedReader in = new BufferedReader(new FileReader("C:/Users/B_Ali/Documents/NetBeansProjects/JavaApplication20/UserNamePassword.txt"));
        String[] s1 = new String[5];
        String[] s2 = new String[5];
        int i = 0;
        while ((s1[i] = in.readLine()) != null) {
            s1[i] = s2[i];
            i = i + 1;

            if (i == 1) {
                Users user1 = new Users(s2[0], s2[1]);
            }
            else if (i == 3) {
                Users user2 = new Users(s2[2], s2[3]);
            }
            else if (i == 5) {
                Users user3 = new Users(s2[4], s2[5]);
            }
        }
        in.close();
    }
    catch (FileNotFoundException ex) {
        Logger.getLogger(LoginScreen.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
        Logger.getLogger(LoginScreen.class.getName()).log(Level.SEVERE, null, ex);
    }

}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    JOptionPane.showMessageDialog(null, user1.getName());
    // TODO add your handling code here:
}
Sign up to request clarification or add additional context in comments.

2 Comments

Again you were declared User user1, user2 in your code
I forgot to seperate the code from the example decleration
2

You need to declare the variables you need outside of the try clause like:

String global = null;

try{
    global = "abc";
    throw new Exception();
}
catch(Exception e){
   if (global != null){
       //your code
   }
}

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.