Results 1 to 10 of 10
Thread: if statement
- 08-10-2012, 12:58 PM #1
Senior Member
- Join Date
- Oct 2010
- Posts
- 139
- Rep Power
- 0
if statement
When the if statement is true it displays the error message as well. It also displays the error message twice when the statement is not true. Any ideas on why and how to fix it?
Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String getPassword = password.getText(); String getUsername = username.getText(); for(int tel = 0; tel<data1.length; tel++) { if((getUsername.equals(data1[tel].getUsername()))&&(getPassword.equals(data1[tel].getPassword()))) { Aktiwiteitupdate up = new Aktiwiteitupdate(); up.welkomnam.setText(getUsername+"!"); this.setVisible(false); } else { JOptionPane.showMessageDialog(null, "Wrong username or password.","ERROR",JOptionPane.ERROR_MESSAGE); } } }
- 08-10-2012, 01:19 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: if statement
For each and every element from your 'data1' array it checks whether it is correct or incorrect; that's what you told it to do ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-10-2012, 01:56 PM #3
Senior Member
- Join Date
- Oct 2010
- Posts
- 139
- Rep Power
- 0
Re: if statement
I changed it to this now and there's still something wrong.
Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String getPassword = password.getText(); String getUsername = username.getText(); int gekry = 0; int tel = 0; while(gekry == 0) { if((getUsername.equals(data1[tel].getUsername()))&&(getPassword.equals(data1[tel].getPassword()))) { Aktiwiteitupdate up = new Aktiwiteitupdate(); tel = tel+1; System.out.println(tel); up.welkomnam.setText(getUsername+"!"); gekry = 1; this.setVisible(false); } else { JOptionPane.showMessageDialog(null, "Wrong username or password.","ERROR",JOptionPane.ERROR_MESSAGE); } } }
- 08-10-2012, 02:11 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: if statement
What does 'still something wrong' mean? Do daemons fly out of your nostrils?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-10-2012, 02:21 PM #5
Senior Member
- Join Date
- Oct 2010
- Posts
- 139
- Rep Power
- 0
Re: if statement
.gif)
Whether I log in with the right username and password or not it displays the error, and it won't go away.
- 08-10-2012, 02:36 PM #6
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
Re: if statement
Your logic is a bit fuzzy. You're checking a list of potential un/pw combos against a single input un/pw. You're set up now to display an error for each time it fails and since it fails to update any counters on that event, you're in an endless loop. If that's the way you need it, put your control loop back in and you need something like this...
int match = 0;
for(tel = 0; tel < data1.len; tel++) {
if(data1[tel] == positive_match) {
match = 1;
break;
}
}
if(match == 1) {
// un/pw has matched so act accordingly
}
else {
// un/pw failed all matches so act accordingly
}Last edited by ics1010; 08-10-2012 at 02:43 PM.
- 08-10-2012, 02:39 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: if statement
You might need to give some more detail here.
What is data1.
What is the loop attempting to do?
It looks to me like you're trying to find the username/password combination in that array?Please do not ask for code as refusal often offends.
- 08-10-2012, 02:44 PM #8
Senior Member
- Join Date
- Oct 2010
- Posts
- 139
- Rep Power
- 0
Re: if statement
Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String getPassword = password.getText(); String getUsername = username.getText(); int verander = 0; for(int tel = 0; tel<data1.length; tel++) { if((getUsername.equals(data1[tel].getUsername()))&&(getPassword.equals(data1[tel].getPassword()))) { verander = 1; } } if(verander == 1) { Aktiwiteitupdate up = new Aktiwiteitupdate(); up.welkomnam.setText(getUsername+"!"); this.setVisible(false); } else { JOptionPane.showMessageDialog(null, "Wrong username or password.","ERROR",JOptionPane.ERROR_MESSAGE); } }
- 08-10-2012, 03:01 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: if statement
I'd split things up in two separate (and simpler methods): 1) a method that tries to find a username/password combination in that array; it could return the index in the array or -1 if nothing was found; 2) a method that takes the appropriate action depending on the supplied username/password combination. That untangles the control flow of the program ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-10-2012, 04:23 PM #10
Senior Member
- Join Date
- Oct 2010
- Posts
- 139
- Rep Power
- 0
Similar Threads
-
Help with IF statement
By Rahim2312 in forum New To JavaReplies: 16Last Post: 05-09-2012, 04:12 PM -
if statement
By Exceedinglife in forum New To JavaReplies: 1Last Post: 04-25-2012, 01:25 AM -
the switch statement and unreachable statement error
By name in forum New To JavaReplies: 2Last Post: 03-26-2012, 04:27 PM -
add an If Else statement and......uh????
By sonny in forum New To JavaReplies: 6Last Post: 03-04-2010, 06:57 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks