Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Norm

Thread: Code not working

  1. #1
    usman400 is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Code not working

    Please check the following code the condition to check invalid attempts only fires once
    i.e. it asks to re-enter user name and password only once, I am supposing it to
    ask 3/4 times as per my code:

    Java Code:
    import java.io.Console;
    public class AtmApp {
    
    	
    	public static String username;
    	public static String password;
    	public static int invalidAttempts;
    	public static Console console = System.console();
    
    	public static void main(String[] args)
    	{
    		invalidAttempts=0;
    
    		dispLogin();
    
    		if(isAuthenticated(username, password))
    		{
    			System.out.println("Welcome");
    		}
    		else
    		{
    			invalidAttempts++;
    			if(invalidAttempts==3)
    			{
    				System.out.println("Too many attemps made !");
    				System.exit(0);
    			}
    			else
    			{
    				System.out.println("");
    System.out.println("Invalid id or password try again " + invalidAttempts);
    				dispLogin();
    			}
    
    		}
    
    	}
    
    
    	private static boolean isAuthenticated(String username2, String password2) {
    		if(username2.equals("ali") && password2.equals("123"))
    			return true;
    		else
    			return false;
    	}
    
    
    	private static void dispLogin() {
    		System.out.print("Enter name: ");
    		username = console.readLine();
    		password = new String (console.readPassword ("Enter pin: "));
    	}
    	
    	
    
    }
    Please help
    Last edited by usman400; 06-05-2012 at 01:44 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default Re: Code not working

    Can you post the contents of the console window from when you execute the program? Add comments to the posting that shows what the problem is and explains what is wrong.

    Why do you think the code will repeat without a loop?
    usman400 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    darraghsan is offline Member
    Join Date
    May 2012
    Location
    Dublin
    Posts
    1
    Rep Power
    0

    Default Re: Code not working

    I'm thinking a do-while loop

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Code not working

    Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    usman400 is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Working On Code
    By maximumammo in forum New To Java
    Replies: 6
    Last Post: 04-04-2012, 04:11 PM
  2. Replies: 1
    Last Post: 03-25-2012, 02:40 AM
  3. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  4. Why is the code not working
    By sanox in forum New To Java
    Replies: 12
    Last Post: 09-08-2009, 11:28 AM
  5. JNI: Why this code not working?
    By playwin2 in forum Advanced Java
    Replies: 5
    Last Post: 11-25-2008, 01:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •