0

Hey I need some help with my code.. I want it to say 'logged on' when the 'Pass' and 'getPass' is the same. and if its not its says 'logged off'.. but it says that all the time even if its correct. :/. Can Someone Help Me :(

p.s

sorry For My Bad English xd

package ca.sidez.main;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class Main extends JFrame {
        private static final long serialVersionUID = 1L;

    private JPanel contentPane;
    private static JTextField txtName;
    private static JTextField txtPass;

    public static String Pass;
    public static String Pass2;
    public static String getPass;
    public static String getName;
    public static String File;

    public Main() {
        try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    setResizable(false);
    setTitle("Login");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(380, 380);
    setLocation(100, 100);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    txtName = new JTextField();
    txtName.setBounds(67, 50, 165, 28);
    contentPane.add(txtName);
    txtName.setColumns(10);

    txtPass = new JTextField();
    txtPass.setBounds(67, 100, 165, 28);
    contentPane.add(txtPass);
    txtPass.setColumns(20);

    JLabel lblName = new JLabel("Login");
    lblName.setBounds(127, 20, 100, 30);
    contentPane.add(lblName);


    JButton btnLogin = new JButton("Login");
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            File = txtName.getText();
            Share();
        }
    });
    btnLogin.setBounds(60, 311, 117, 29);
    contentPane.add(btnLogin);

 }

public static void Share() {
    try {
//Checks if The Username Exists.
        File file = new File(File + ".txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuffer stringBuffer = new StringBuffer();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuffer.append(line);
            stringBuffer.append("\n");
        }
        fileReader.close();
        Pass = stringBuffer.toString();
        getPass = txtPass.getText();
        getName = txtName.getText();
        System.out.println("Clients Password: " + getPass + " For Acc '" + getName + "' ");
        System.out.println("The Correct Password For Acc:  " + getName + " Is: " + Pass);
        if(Pass == getPass) {
            System.out.println("Logged In");
        } else {
            System.out.println("Logged Out");
        }
    } catch (IOException e) {
        System.out.println("Wrong Username Or Password");
    }
   }

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Main frame = new Main();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
}
2
  • 2
    possible duplicate of How do I compare strings in Java? Commented Aug 1, 2014 at 19:58
  • 7
    One day there will be a "spot the bug" olympics, it will have a string reference comparison and I will win the damn trophy for that. Commented Aug 1, 2014 at 19:59

2 Answers 2

2

There are probably many issues with the code you have posted but one of them, and perhaps this is why it is not working as you think it should, is the adding of a newline to the stringbuffer:

     stringBuffer.append(line);
     stringBuffer.append("\n");

This would make the password in the file "mySecretPassword\n" which will not evaluate to true when compared to "mySecretPassword" using Equals (assuming that is the password in the file and what the user entered in the password field).

Your code would also not work if there is more than one line in the password file. If the assumption is that there will be only one line in the file and it is the password to verify against, then read only one line.

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

1 Comment

Yeah That Was The Problem. and yes i will only have one line in that text file :) thanks for the help
1
if(Pass == getPass)

Should really be:

if(Pass.compareTo(getPass) == 0)

or

if(Pass.equals(getPass))

Are how you actually compare the content of the string.

if(Pass == getPass)

Is how you compare to see if they are both references to the same object.

Another Problem

while ((line = bufferedReader.readLine()) != null) {
    stringBuffer.append(line);
    stringBuffer.append("\n");
}
fileReader.close();
Pass = stringBuffer.toString();

Adds a new line to the end of Pass

You just want

while ((line = bufferedReader.readLine()) != null) {
    stringBuffer.append(line);
}
fileReader.close();
Pass = stringBuffer.toString();

11 Comments

I think its because I tried to upvote my own answer so it gave me 3 down votes as punishment.
a few ideas: 1) he answered a poor question, 2) people protest to using .compareTo() == 0 instead of .equals() ?
@KevinL People who answer "poor questions" shuldn't get donvoted. People who ask questions that could be answered with one google search should be donvoted.
Meh I'm giving this answer a +1 until someone points out why it's so awful.
@Gumbo answering poor questions encourages the behavior of asking poor questions, see meta discussion meta.stackoverflow.com/a/255462/3666763 The community isn't completely in agreement on this, but as i said, those were ideas
|

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.