Solved

Need help checking an array against input

Posted on 2003-12-10
8
497 Views
Last Modified: 2006-11-17
The code below runs but when I enter any password other than the first in the array, I get Access Denied.  As long as it is in the array, it should show Access Granted.  Any ideas?


import javax.swing.*;
import java.awt.*;
import java.awt.event.*; //Needed for ActionListener
public class JPasswordC extends JApplet implements ActionListener
{
 JLabel greeting = new JLabel("Please enter password and press Enter"); //Creates new label asking for input  

 JTextField answer = new JTextField("",10); //Creates the empty box for user to input password
 FlowLayout flow = new FlowLayout();  //Sets flow layout

 public void init()
 {
  Container con = getContentPane();
  con.add(greeting);  // Adds the greeting
  con.setLayout(flow);  // Sets layout to flow
  con.add(answer);  // Adds the input box
  answer.addActionListener(this);  //Listens for user input
  answer.requestFocus();  //Places focus on the input of the user from above
 }

 public void actionPerformed(ActionEvent psdEvent)
 {
  Object source = psdEvent.getSource();
  if (source instanceof JTextField)//start if to see if input matches any in string below
  {
   String[] passwords = {"Rosebud","Redrum","Surrender","Jason","Dorothy"}; //create array to compare
   String input = answer.getText(); //Sets user input(answer) to variable input

   for(int i=0; i < passwords.length; i++)//for loop goes through the array
   {
    if (input.equalsIgnoreCase(passwords[i]))
    {
     JOptionPane.showMessageDialog(null, "Access Granted");
     break;
    }
    else
    {
     JOptionPane.showMessageDialog(null, "Access Denied");
     break;
    }
   }
  }
 }
}
0
Comment
Question by:angelori
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9917856
should work
check you aren't running a old cached class (restart your browser).
0
 
LVL 92

Expert Comment

by:objects
ID: 9917874
sorry no it won't :)
 you don't want to popup the dialog inside the loop, as this will just check the 1st array element. Instead you want to do it after the loop according to whether a match was found or not.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 275 total points
ID: 9917886
  String match = null;
   for(int i=0; match==null && i < passwords.length; i++)//for loop goes through the array
   {
    if (input.equalsIgnoreCase(passwords[i]))
    {
      match = passwords[i];
    }
   }

    if (match!=null)
    {
     JOptionPane.showMessageDialog(null, "Access Granted");
     break;
    }
    else
    {
     JOptionPane.showMessageDialog(null, "Access Denied");
     break;
    }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
InNoCenT_Ch1ld earned 75 total points
ID: 9918474
just to explain objects's comment.
your code:

   for(int i=0; i < passwords.length; i++)//for loop goes through the array
   {
    if (input.equalsIgnoreCase(passwords[i]))
    {
     JOptionPane.showMessageDialog(null, "Access Granted");
     break;
    }
    else
    {
     JOptionPane.showMessageDialog(null, "Access Denied");
     break;
    }
   }
 
for the first time, it will check if the password is match, if yes, it shows the dialog; if not, the it will enter the else{}.. you have a "break" there and it will end this for loop and "jump" out of it, so it wont check for the following password in the array... ;)

so to make it work, just go with objects comment (the code he posted).

-Ch1ld-
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9919675
You should probably use a JPasswordField for a password:

JPasswordField answer = new JPasswordField(10);
...

String input = new String(answer.getPassword());
0
 

Author Comment

by:angelori
ID: 9920152
Thanks guys!  You rock!
0
 

Author Comment

by:angelori
ID: 9920257
I hope the points got split right for this....I don't know why the answers show up as assited/accepted...I thought it would have shown the other way around.  Did you get your points, objects?  
0
 
LVL 92

Expert Comment

by:objects
ID: 9924194
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question