Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help checking an array against input

Posted on 2003-12-10
8
Medium Priority
?
511 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 1100 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 3

Accepted Solution

by:
InNoCenT_Ch1ld earned 300 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

670 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