• Post Reply Bookmark Topic Watch Topic
  • New Topic

Register/Login Swing Application error in functionality  RSS feed

 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I have big trouble understanding why my code does not work
I have created a simple login/register in java but
when i click login button it always states that account is incorrect, when i click register it registers a new account with the same username(while i have placed many restrictions to avoid it)
Can you guys maybe spot the bad logic in my code?
Here is my code
The 2 actionlisteners


and is the class var i am using to store things


the method user has a String, a char[] and a boolean with getters and setters.
Also I am passing var variable from main(initialization and declaration) to every class and I plan to load/save it as a file when i succeed making it work.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't analyzed all your code but I've noticed that you use == to compare Strings. Don't do that. Use equals() method instead.

Avoid The Equality Operator

By the way, don't write
Write:
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
God did not knew that == and equals() can produce different results.
I am a junior college programmer and I have never seen any of my instructors using equals.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't edit my first post, by changing the == to .equals() it fixed the problem on my Registration ( when i logout and try to create a new user with the same username the error is displayed).
I also forget to post that method which is used in Login actionlistener (where the problem still persist)
and after your very nice comment on return condition;
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing about the naming convention in Java. Variable names should start with lower case. So, password and username, not Password and Username.

More important thing. If you want to compare contents of arrays (like your psw and Password), then don't use the equals() instance method. Use Arrays.equals() static method instead.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java convention is that class names start with an upper case letter and that variable names should begin with a lower case character.

Variable names and class names should be meaningful; "var" is not.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays.equals() seems to fix the password problem and now login/register fully works. Thanks for the comments guys, I am going to rename my classes and variables accordingly and keep doing it this way from now.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out Another thing I noticed is that you have a method with Boolean return type, which is an object. You probably wanted to get a boolean, which is a primitive. Because of boxing conversion, you won't notice any difference in functionality, but under some circumstances that B might cause performance problems.
If you look here in the Java® Language Specification and here about the Object class, you can, I hope, work out that
array1.equals(array2)
only tests whether the two are the same object, using ==. What if we didn't call == the equality operator? Maybe it would be better to call it the same object operator. Just maybe.
If you look in those links, and in Arrays#equals(), you don't find anything about equals() using == on arrays. I think they presume we know that about un‑overridden equals() methods.

By the way: If you go through the documentation for a class, like this one, and you find it says equals in the section headed
Methods inherited from class java.lang.Object
then you can tell it hasn't been overridden.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angelos Naoumis wrote:I have never seen any of my instructors using equals.

Wow - this should be one of the first things mentioned when studying about class instances.

You use == to check whether two object references point to the same instance.

You use equals() to determine whether two object instances have the same content (or are considered to be equivalent).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!