• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Boolean problem

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using a static int for the counter, and calling a field i both look suspect to me.
 
Narendira Sarma
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. I am not good with Swing. Someone else can help you.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it/s now a Swing question, that would sit better on the Swing forum. Moving
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree. That sentence wasn't expressed clearly and is difficult to understand.
 
Peter Granstrom
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic