Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    uppercase or lowercase

    hi, My code is runnig perfect but I am missing a detail, and I have no idea how to. The situation is when you input the password does not matter if it is upper o lowercase it will say "access granted" otherwise will say "access deined"

    Thanks


    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class JPasswordB extends JApplet implements ActionListener
    {
    
    	Container con = getContentPane ();
    	JLabel please = new JLabel ("Please Enter Password");
    	Font headlineFont = new Font("Helvetica", Font.BOLD, 36);
     	JButton enter = new JButton("Click to Enter");
    	JLabel passtyped = new JLabel ("");
    	String password = "Rosebud";
    	JPasswordField fieldpassword = new JPasswordField(8);
    
     	public void init()
    	{
    		please.setFont(headlineFont);
    		con.add(please);
    		con.add(fieldpassword);
    		con.add(enter);
    		con.setLayout(new FlowLayout());
    		enter.addActionListener(this);
    		fieldpassword.addActionListener(this);
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    		String name = fieldpassword.getText();
    		if 
    		( name.equals ( password ) ) 
    			{       
    			passtyped.setText("Access granted");
    			con.add(passtyped);
    			}
    				else
    				{                              
    					passtyped.setText("Access Denied");
    					con.add(passtyped);
    		  			validate();
    				}
    	}	
    }

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    Not sure I caught the question there. Right now it looks like your code will be case sensitive, if you want to change that, I would suggest looking at the String class in the javadocs.

    http://java.sun.com/j2se/1.5.0/docs/...ng/String.html


    Everything you need for comparing strings will be right there, including comparing them while ignoring the case.

  • #3
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    are you saying you WANT that functionality or you don't have that functionality? Or you do have that functionality and you don't want it?

    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #4
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    uppercase or lowercase

    My apologies for not being clear enough.

    The idea is when you put the right password it does not matter if it is upper or lower case, the program will say "access granted" but if I put the wrong password (either lower or uppercase) it will say "access denied"

    Thanks and sorry again.

  • #5
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    If you look at the link posted by brad, you'll see a few equals functions for the string class.


    One of these functions is called equalsIgnoreCase(String) You can use this function (rather then equals) to do what you are looking for.

    My suggestion though is that you visit the link posted by brad, as it is best that you get as familiar with the Java API as soon as possible (especially if you plan on continuing to program in Java.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #6
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I am still working on that but know I have an additional step. It will be to do that with 5 different passwords. Could you help me?

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class JPasswordC extends JApplet implements ActionListener
    {
    
    	Container con = getContentPane ();
    	JLabel please = new JLabel ("Please Enter Password");
    	Font headlineFont = new Font("Helvetica", Font.BOLD, 30);
     	JButton enter = new JButton("Click to Enter");
    	JLabel passtyped = new JLabel ("");
    	String[] passwords = {"Rosebud","Redrum","Jason","Surrender","Dorothy"};
    	JPasswordField fieldpassword = new JPasswordField(12);
    
     	public void init()
    	{
    		please.setFont(headlineFont);
    		con.add(please);
    		con.add(fieldpassword);
    		con.add(enter);
    		con.setLayout(new FlowLayout());
    		enter.addActionListener(this);
    		fieldpassword.addActionListener(this);
    		
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    		String name = fieldpassword.getText();
    		if
    		( name.equals ( passwords ) ) 
    			{       
    			passtyped.setText("Access granted");
    			con.add(passtyped);
    			validate();
    			}
    				else
    				{                              
    					passtyped.setText("Access Denied");
    					con.add(passtyped);
    		  			validate();
    				}
    	}	
    }


  •  

    Posting Permissions

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