• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Getting Output for Else Statement Despite Username and Password Being Correct  RSS feed

 
Greenhorn
Posts: 2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I need your help.

When running the project, I'm getting the output for my else statement instead of my if statement even though the username and password I've created are being entered correctly into the GUI.
Any advice on how I can correct this problem would be greatly appreciated.

Thanks! :-)
Login-1.2.JPG
[Thumbnail for Login-1.2.JPG]
 
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please post the actual code rather than an image, and use Code Tags so that it will be nicely formatted.

 
Bartender
Posts: 5641
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not use screen snapshots for source code or error text. Cut and paste them into a post instead.

Your problem is with the two small methods at the top. You've declared LOCAL String variables and when the method ends the contents disappear. I assume you meant to save it in an instance variable so that you can retrieve it later.
 
Marshal
Posts: 6594
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron is right.

The problem is, that you cannot compare String and char[] array by equals method that way as they are incomparable.

2 options you can try to apply to overcome this problem:
1. assemble a String from char array and then compare password string with an assembled string from char array
2. password string convert to a char array and compare against input array with Arrays.equals(...)

 
lowercase baba
Posts: 12725
51
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you are sure you are doing things right, that's when you most need to check yourself.  don't be afraid to use temporary log messages to validate.  you can print to a screen or a log file the values you think you are testing...if you are sure your string is correct - print it.  I usually print it between two characters, like a colon or a pipe, to be sure there aren't leading/trailing spaces or carriage returns.
 
Justyn Jackere
Greenhorn
Posts: 2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda, I chose the 1st option you suggested and it worked perfectly. Thank you for making that suggestion.

Also, I apologize for posting a screen-shot of my code instead of copying my code directly into the post. I'm new to these forums and still have much to learn about proper etiquette. :-)
 
Marshal
Posts: 63454
207
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you know that it is insecure to compare passwords, so I think this must simply be an exercise about inputs.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!