473,468 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Showing multiple information messages

lotus18
866 Contributor
Hello world.

I'm trying to teach myself and I'm having trouble with this simple codes. I just want to show if the user typed the username and the password correctly, Now the problem goes like this, it displays multiple information messages if both the username and the password are incorrect or if they are both null. And also, at line 36 I'm having trouble too. Please guide me : )

Expand|Select|Wrap|Line Numbers
  1. String UserName = "reysean";
  2.         String Password = "lotus18";
  3.         boolean CheckUserName=false;
  4.         boolean CheckPassword=false;
  5.  
  6.         //Check for username
  7.         if (txtUserName.getText().isEmpty()){
  8.             JOptionPane.showMessageDialog(null,"Please enter username.");
  9.             CheckUserName=false;
  10.             txtUserName.grabFocus();
  11.         }
  12.         else if (txtUserName.getText().equals(UserName)) {
  13.             CheckUserName=true;
  14.         }
  15.         else{
  16.             JOptionPane.showMessageDialog(null,"Please enter correct username.");
  17.             CheckUserName=false;
  18.             txtUserName.grabFocus();
  19.         }
  20.  
  21.         //Check for password
  22.         if (txtPassword.equals("")){
  23.             JOptionPane.showMessageDialog(null,"Please enter password.");
  24.             CheckPassword=false;
  25.             txtPassword.grabFocus();
  26.         }
  27.         else if (txtPassword.equals(Password)) {
  28.             CheckPassword=true;
  29.         }
  30.         else{
  31.             JOptionPane.showMessageDialog(null,"Please enter correct password.");
  32.             CheckPassword=false;
  33.             txtPassword.grabFocus();
  34.         }
  35.  
  36.         if ((CheckUserName=true) && (CheckPassword=true)){
  37.             JOptionPane.showMessageDialog(null, "The username is: " + UserName + "\n The password is: " + Password );
  38.         }
Rey Sean
Mar 27 '08 #1
9 1685
r035198x
13,262 MVP
What is the purpose of the final else blocks in your chains of if-else?
e.g
Expand|Select|Wrap|Line Numbers
  1.           ...else{
  2.             JOptionPane.showMessageDialog(null,"Please enter correct username.");
  3.             CheckUserName=false;
  4.             txtUserName.grabFocus();
  5.         }
Is already handled by the first if part. That's why you have many messages.

For the final part, realize that
1.) == is not the same as =
2.) if you have a boolean variable called test, then
if(test == true) is the same as if(test)
Mar 27 '08 #2
lotus18
866 Contributor
In VB, I can use Exit Sub for this after prompting a messages (It will exit the next line). Is there any alternative for this?
Mar 27 '08 #3
JosAH
11,448 Recognized Expert MVP
In VB, I can use Exit Sub for this after prompting a messages (It will exit the next line). Is there any alternative for this?
Yes, in grown up normal programming languages we use 'return'.

kind regards,

Jos ;-)
Mar 27 '08 #4
lotus18
866 Contributor
Yes, in grown up normal programming languages we use 'return'.

kind regards,

Jos ;-)
Oh I see. I've forgot to use return. Thanks JosAH.

Rey Sean
Mar 27 '08 #5
lotus18
866 Contributor
Ahh.. Sorry guys, i can't still make it. I can't see this message even if both the username and password are correct.:

Expand|Select|Wrap|Line Numbers
  1. JOptionPane.showMessageDialog(null, "The username is: " + UserName + "\n The password is: " + Password );
Any ideas?

What I did was (As r035198x's suggestion at post #2).

Expand|Select|Wrap|Line Numbers
  1.  if ((CheckUserName) && (CheckPassword)){
  2.         JOptionPane.showMessageDialog(null, "The username is: " + UserName + "\n The password is: " + Password );    
  3.         }
Rey Sean
Mar 28 '08 #6
r035198x
13,262 MVP
Post the code that you have now.
Mar 28 '08 #7
lotus18
866 Contributor
This is my code:

Expand|Select|Wrap|Line Numbers
  1. String UserName = "reysean";
  2.         String Password = "lotus18";
  3.         boolean CheckUserName=false;
  4.         boolean CheckPassword=false;
  5.  
  6.         //Check for username
  7.         if (txtUserName.getText().isEmpty()){
  8.             JOptionPane.showMessageDialog(null,"Please enter username.");
  9.             CheckUserName=false;
  10.             txtUserName.grabFocus();
  11.             return;
  12.         }
  13.         else if (txtUserName.getText().equals(UserName)) {
  14.             CheckUserName=true;
  15.             //goto CheckPassword;
  16.             //txtUserName.grabFocus();
  17.         }
  18.         else{
  19.             JOptionPane.showMessageDialog(null,"Please enter correct username.");
  20.             CheckUserName=false;
  21.             txtUserName.grabFocus();
  22.             return;
  23.         }
  24.  
  25.         //Check for password
  26.         if (txtPassword.equals("")){
  27.             JOptionPane.showMessageDialog(null,"Please enter password.");
  28.             CheckPassword=false;
  29.             txtPassword.grabFocus();
  30.             return;
  31.         }
  32.         else if (txtPassword.getPassword().equals(Password)) {
  33.             CheckPassword=true;
  34.         }
  35.         else{
  36.             JOptionPane.showMessageDialog(null,"Please enter correct password.");
  37.             CheckPassword=false;
  38.             txtPassword.grabFocus();
  39.             txtPassword.selectAll();
  40.             return;
  41.         }
  42.  
  43.         if ((CheckUserName) && (CheckPassword)){
  44.         JOptionPane.showMessageDialog(null, "The username is: " + UserName + "\n The password is: " + Password );    
  45.         }
Mar 28 '08 #8
JosAH
11,448 Recognized Expert MVP
Why are people so afraid to create a few small methods? Your logic boils down
to the simple (pseudo code):

Expand|Select|Wrap|Line Numbers
  1. if (!(username.equals(inputUsername()) & password.equals(inputPassword())))
  2.    showIncorrectInput();
  3. else
  4.    showCorrectInput();
  5.  
Note that you should never implicitly tell a cracker that something is correct, so
the showIncorrectInput() simply shows that something was incorrect. Also note
that I used a single & instead of a double && so that no matter whether the
username was incorrect, the program still asks for a password.

Now you need to implement the following simple methods:

- inputUsername()
- inputPassword()
- showIncorrectInput()
- showCorrectInput()

I don't consider their implementation rocket science.

kind regards,

Jos
Mar 28 '08 #9
lotus18
866 Contributor
Sorry for the late reply. hehe... We had just arrived from our short vacation. I did it on that way to show me how that simple program flows. Yes, you are right, the crackers must not have a hint to crack usernames and passwords. Thanks for guiding me Joash.

Rey Sean
Mar 30 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Simone | last post by:
Hi all, I am new to python and trying to write a simple GUI that would call 2 growisofs processes (dvd burning) at the same time, and retrive the exit code to be printed on a text box (couldn't...
2
by: Simone | last post by:
Simone wrote: >Hi all, I am new to python and trying to write a simple GUI that would >call 2 growisofs processes (dvd burning) at the same time, and retrive >the exit code to be printed on a...
1
by: tilmann | last post by:
We are developping an application. The application is written in Cobol, for the database the client can use either a DB2 UDB on Unix systems or a DB2 V 7 on z/OS. The application itself can run on...
6
by: mark | last post by:
I have an asp.net ecommerce web application on a remote web server. I'm using an Access database on the back end. I've notice a few strange things. When I mimic an multiple user environment by...
2
by: Robert Smith | last post by:
Hello, I have a problem with my progress bar, as shown in the attached code, the values on the bar are incremented within a threaded timer event. The timer works fine and ticks all the way...
1
by: Diego F. | last post by:
I use to show the error messages that I get in a try block. try { ... } catch (Exception ex) { ... (ex.Message); }
1
by: quill | last post by:
Hi I am making a chatroom script and it appears that the problem seems to be that my setTimeout's are conflicting. The logic is as follows: Run a login check every x seconds Run a trigger...
6
by: MaiyaHolliday | last post by:
Hello, I've recently installed apache on a new computer, and cannot figure out why my site will not process any includes. (it was working on my old one) There are no errors on the page such as...
2
by: jaxpylon | last post by:
I'm having trouble with a query and am wondering if someone can help me. What I'm doing, is getting a list of topics for a "category" from a database for a forum. For each topic, I need its details...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.