Solved

Need help checking an array against input

Posted on 2003-12-10
8
502 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

751 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