This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Boolean problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Boolean problem" Watch "Boolean problem" New topic
Author

Boolean problem

Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
I'm implementing a login form, with two inputs, allowing a maximum of 3 tries for authentication; username and password. Scanner class reads input from notepad, and matches it with input from user. However, without a boolean statement, the program will search every line for a match until file ends, displaying every error message until match is found. For example, if the username/password combination is on the 5th line, it will display 4 error messages (invalid username/password) before allowing the user to login. Once boolean statements in the code are implemented, it matches the username/password combination immediately. However, the error message for wrong combination of username/password does not work anymore.
Can anyone please help check if the boolean statement is done properly? Code is as below.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I can't understand what you are trying to achieve here. i is a local variable so how can it store how many times the user has entered the wrong login details. You need to declare it at the class level. Also why are you using all the logic to show the next page or exit the application in the loop.



if we take a look at this statement, then you are using continue thus the loop will continue to check the username and password even after a message is displayed to the user. And you never set ok to true so this code won't be executed anyways. Think about it a little more. You are using the boolean field as a flag to indicate whether the login details were right but you are not setting the value of the flag anywhere...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
shiva shankar
Ranch Hand

Joined: Jun 10, 2001
Posts: 84
This will not work. I assume actionPerformed() will be called repeatedly for 3 times since its 3 retries, and you need to maintain the retrycount somewhere. You cannot declare the retry count also as static since there will be many threads calling this method. If that retaining that retryCount logic is solved the other code is very straight forward.

- Shiva

Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Ankit >>

Boolean has been set as false in default , thus "ok!=false" will return "true" no?
The reason why i set a boolean is because i assumed the program to stop and allow the users to try 3 times. Without the booleans , the program will only allow the user to try once, and the program pops up 3 error messages consequently instead of one, and then exits.

I don't understand what it means by declaring 'i' in the class level.
The first part of the boolean worked, as in..it checks every line of the input (notepad) and allows authentication. However if the wrong username/password is keyed in, it does not generate an error message as it is expected to do so.

This is part of the original code before i added in the boolean. If there are any better suggestions on to fulfill the criteria of 3 try-authentication, can i have some examples along with it, just for better understanding?

Narendira Sarma
Greenhorn

Joined: Nov 14, 2008
Posts: 18
In your code posted above, you are displaying the Error message after each combination is checked from the text file.

Instead of showing the Error message in the else part, you can have a boolean variable assigned to false. If a match is found assign the boolean as true.

After the while loop finishes, check for the boolean and if it is false, display the Error message. This way you will display the error message only once.

Hope I am clear. I will write something like below.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Don't use == true or != true or == false or != false. You have written that ok!=false returns true, which I think may be mistaken, so you may be getting confused by what is very confusing coding.

Also, you can get nasty errors if you write = instead of ==.

If you want something to be true, you write if (ok) . . . and to get false you write if (!ok) . . .

Don't writeYou can simply write
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Yeah, the code is working already. My bad, got a lil confused on how boolean works lol ..How do i count tries? Like , lets say the codes is suppose to allow 3 tries for login. Is this possibly done also with boolean? What is retryCount?
Narendira Sarma
Greenhorn

Joined: Nov 14, 2008
Posts: 18
You must have a global variable (or a variable outside the method) and increment it each time you go into the if(!found) block.

So while checking for username and password, you have check for the count variable also.

like



to go upto number 3, you cant have a boolean to do that. You need to use int to do that.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Luen Hong Wuan wrote:Ankit >>

Boolean has been set as false in default , thus "ok!=false" will return "true" no?
The reason why i set a boolean is because i assumed the program to stop and allow the users to try 3 times. Without the booleans , the program will only allow the user to try once, and the program pops up 3 error messages consequently instead of one, and then exits.



Yeah, I agree with Campbell, but anyways I just wanted to comment on the above.

The highlighted remark cannot possibly be correct, cause that would mean that ok could be both false and not false at the same time. true?

in other words...

( (ok==false) & (ok!=false) ) == false returns true

Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Fred >>
Haha yeah, now it made sense =.="

Narendira>>
Hmm..what about the loop? If I were to set an increment value to allow 3 tries of login, it also means, that the loop must cover all the way up to the input right? To allow users to retype and the program to recheck? Darn, to figure out where to place the loop and avoid reload is definitely a tough nut to crack =.="



Current code looks like this. I've also set int i as a static value way up at class level. However, its confusing on which loop to use, and also the placing of loop start and end.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Using a static int for the counter, and calling a field i both look suspect to me.
Narendira Sarma
Greenhorn

Joined: Nov 14, 2008
Posts: 18
I am assuming your actionPerformed() method is overridden method and is called on a button click event. Right?

Can you point us out at how the control comes into the actionPerformed() method? I mean, when is the actionPerformed() method is called?
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Oh, ok. The actionperform method takes place when the Login button is pressed. As far as im concerned, its tasks is suppose to include ;

checking on whether the username/password combination is correct, before allowing NextPage to take place, and also to limit the number of tries to 3 times before calling the system off.

I've achieved the first task, which leaves limiting number of tries to 3 times before calling the system off. I really wish someone could show me an example of a proper way to increment the count without reloading any other functions.
Narendira Sarma
Greenhorn

Joined: Nov 14, 2008
Posts: 18
I am assuming your actionPerformed() method is in the same class as the calling method.

Please have a global variable at class level (i am calling it tries) and initialize it to 1

in your if(!found) block increment it. tries++;

and your

will look like this



The logic is you will increment tries count every time username and password are not valid.

And while checking for username and password, you will also check for number of tries.

Hope its clear! All the best
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
It worked. Thanks Narendira for the thorough explanation, I've learned alot. I really never knew increment had to be done like that do avoid process from reloading. I was definitely trying very hard to declare a do-while or for loop to increment the value, but I think that it can be done with a do-while or a for loop if the "i" value is static at class level right? This is just out of curiosity ^^

I would also like to ask, if the program validated the username and password, it opens up NextPage, but the first Login page remains there, along with its Login and Exit function. How can I disable the first page after login is successful?

I cannot use setVisible(false) for login page, because the frame is created under static void main. The whole updated code is as below, I know its long, but I'm sure you guys will get a better idea what I'm talking about.

Narendira Sarma
Greenhorn

Joined: Nov 14, 2008
Posts: 18
Sorry. I am not good with Swing. Someone else can help you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
If it/s now a Swing question, that would sit better on the Swing forum. Moving
Peter Granstrom
Greenhorn

Joined: Jun 08, 2009
Posts: 10
Narendira Sarma wrote:In your code posted above, you are displaying the Error message after each combination is checked from the text file.

Instead of showing the Error message in the else part, you can have a boolean variable assigned to false. If a match is found assign the boolean as true.

After the while loop finishes, check for the boolean and if it is false, display the Error message. This way you will display the error message only once.

Hope I am clear. I will write something like below.




Hi i liked your answer,
but i think in the if() i would try to decide if boolean to true or false before
because then you dont have to make changes afterwards and can decide
before if its right or wrong and probably get a better structure in your code
Narendira Sarma
Greenhorn

Joined: Nov 14, 2008
Posts: 18
Peter Granstrom wrote:
Hi i liked your answer,
but i think in the if() i would try to decide if boolean to true or false before
because then you dont have to make changes afterwards and can decide
before if its right or wrong and probably get a better structure in your code


Hi Peter,

I couldn't understand what you were trying to say. I am a rookie programmer myself. That being said, I showed him how "I" would have handled the situation.

But I am eager to learn. Could you please tell me more clearly?

Thanks
-Naren
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Agree. That sentence wasn't expressed clearly and is difficult to understand.
Peter Granstrom
Greenhorn

Joined: Jun 08, 2009
Posts: 10
Narendira Sarma wrote:
Peter Granstrom wrote:
Hi i liked your answer,
but i think in the if() i would try to decide if boolean to true or false before
because then you dont have to make changes afterwards and can decide
before if its right or wrong and probably get a better structure in your code


Hi Peter,

I couldn't understand what you were trying to say. I am a rookie programmer myself. That being said, I showed him how "I" would have handled the situation.

But I am eager to learn. Could you please tell me more clearly?

Thanks
-Naren


maybe if able to decide boolean to true or false before the if ()
then you might be able to use a more flexible code .. im not sure thou
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Boolean problem
 
Similar Threads
JRadiobutton problem
Determining the location of the file
connecting java application to JAVADB database
java.lang.ClassCastException
Implementing scanner into a simple login page