aspose file tools*
The moose likes Beginning Java and the fly likes if and if else code problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if and if else code problem" Watch "if and if else code problem" New topic
Author

if and if else code problem

lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47


In this code if I input 'N' or 'n' the register method get called.But if I input 'l' or 'L' the login method doesnt get called.I swapped
"if "and "if else" code parts and tried again.That time the Login method did get called ,and register method wasn't activated.So the problem is with the if else code parts .Not with those methods code parts.Please Someone help me to pint out this error.It may bea very simple one.But cannot figure out it at the moment.


[Bsc in Computer Science and Engineering] [SCJP 80%][BCS certificate level]
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi lakmal,

Why dont you try storing the output of input.next().charAt(0) and then using that variable in the comparinson instead of calling input.next().charAt(0) everytime.

The repeated call to input.next().charAt(0) could be the problem.
I am guessing.

Priety.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Well, assuming that input is a scanner object... basically your code... gets a token from the user and checks if the first letter is "N", if it isn't, it will get another token from the user and checks if the first letter of this new token is "n", if it isn't, it will get another token from the user and checks if the first letter of this new token is "l", if it isn't... so, if you want an "L", you will have to enter it 4 times, with spaces in-between, so that your condition will pass its test. Is this what you wanted?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Agree with Henry.

But, R_ for login, then looking for L?

Maybe you ought to alter your prompts, too.
lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
Thanks for all you people who replied for my question.And after posting my question I found a way to solve my problem.It was exactly the same method Preity has mentioned.And Henry,Yes You are correct about that.Thanks for that valuable tip.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
You're welcome
 
wood burning stoves
 
subject: if and if else code problem