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();
            }
        }
    });
}
}
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
Sidezpro
  • 11
  • 6
  • 2
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jeroen Vannevel Aug 01 '14 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. – Jeroen Vannevel Aug 01 '14 at 19:59

2 Answers2

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.

user469104
  • 1,206
  • 12
  • 15
  • Yeah That Was The Problem. and yes i will only have one line in that text file :) thanks for the help – Sidezpro Aug 01 '14 at 20:20
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();
Aaron
  • 171
  • 10
  • For those of us who don't know Java, why is this getting downvoted? – Mike Christensen Aug 01 '14 at 20:03
  • 3
    I think its because I tried to upvote my own answer so it gave me 3 down votes as punishment. – Aaron Aug 01 '14 at 20:04
  • 2
    a few ideas: 1) he answered a poor question, 2) people protest to using `.compareTo() == 0` instead of `.equals()` ? – Kevin L Aug 01 '14 at 20:05
  • i have tryd .equals() but it doesent work either..:/ – Sidezpro Aug 01 '14 at 20:08
  • 1
    @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. – Gumbo Aug 01 '14 at 20:08
  • 2
    Meh I'm giving this answer a `+1` until someone points out why it's so awful. – Mike Christensen Aug 01 '14 at 20:09
  • 1
    @Gumbo answering poor questions encourages the behavior of asking poor questions, see meta discussion http://meta.stackoverflow.com/a/255462/3666763 The community isn't completely in agreement on this, but as i said, those were _ideas_ – Kevin L Aug 01 '14 at 20:10
  • Answers like this add no value compared to the one that they duplicate and act as an encouragement to new users since they get their question answered before it even gets closed. Closevote it and if you don't have the rep then you can flag it. – Jeroen Vannevel Aug 01 '14 at 20:11
  • @KevinL Well, didn't think of it this way. – Gumbo Aug 01 '14 at 20:12
  • @Sidezpro why do you "stringBuffer.append("\n");" you then make Pass = stringBuffer.toString your Pass probably has a \n at the end of it – Aaron Aug 01 '14 at 20:12
  • THANKS @Aaron :D i dident realise that xD – Sidezpro Aug 01 '14 at 20:16