...

View Full Version : uppercase or lowercase



nogal
04-15-2008, 05:45 PM
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





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();
}
}
}

brad211987
04-15-2008, 06:24 PM
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/api/java/lang/String.html


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

Aradon
04-15-2008, 08:26 PM
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?

:confused:

nogal
04-15-2008, 08:32 PM
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.

Aradon
04-15-2008, 08:57 PM
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.

nogal
04-16-2008, 12:34 AM
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?




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();
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum