Solved

Need help checking an array against input

Posted on 2003-12-10
8
466 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IT Company 5 69
topping1 challenge 7 51
Systems talking to each other 5 109
base64 decode encode 12 96
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now