Solved

Need help checking an array against input

Posted on 2003-12-10
8
507 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

632 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