如何将两个输入值与两个数组

时间:2017-04-11 04:15:23

标签: java arrays swing

我是一名试图在线课程中完成作业的noob java程序员。我特别提到这一点,因为课程材料没有以这种方式比较字符串,教授没有回复我的电子邮件。

我需要获取用户输入的用户名和密码,如果两者都正确则返回结果。我查看了Oracle's Password Fields以及此处comparing one input to one array上的类似问题,以及我谷歌搜索中出现的其他一些帮助帖子。

如果我输入第一个名字和密码,我当前的代码将返回正确,但是当我尝试第二个名称和密码时,它根本不会返回结果。即使只需要一个输入值也是正确的,它也会返回true。根据作业,我使用的是swing应用程序,所以我先发布相关代码,然后发布下面的整个内容,以避免混淆。

对我失踪的任何帮助都将不胜感激。

麻烦部分:

// get the values entered by the user
String str1 = name1.getText();
String str2 = pass1.getText();
//create my user info strings
String[] userNameArray = {
  "Vale.Vicky",
  "Lane.Lois",
  "Kent.Clark",
  "Wayne.Bruce",
  "Parker.Peter",
  "Rogers.Steve",
  "Luther.Lex",
  "Osborn.Harry",
  "Prince.Diana",
  "Admin"
};
String[] passwordArray = {
  "BatmanFan1",
  "SuperBoyFan1",
  "Kryptonite1",
  "Batman1",
  "SpiderMan1",
  "CtAmerica1",
  "Letitia1",
  "NewGoblin1",
  "WonderWoman1",
  "Password1"
};

boolean check = true;

//Test for matching user name
while (true) {
  for (String i: userNameArray) {
    if (str1.equals(i)) {
      checked.setText("Valid Login");
      check = false;
      break;
    }
  }
  //Test for matching password
  for (String j: passwordArray) {
    if (str2.equals(j)) {
      checked.setText("Valid Login");
      check = false;
      break;
    }
  }
  //return if both false
  if (check) {
    checked.setText("Invalid Login, Try Again");
    continue;
  }
  break;
}
}

整个项目代码:

/**
 *Rai Duffy
 *Code Creation, Troubleshooting, and Validation Tests
 */

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;

public class logInChecker {

  private JFrame frmWeek;
  private JTextField pass1;
  private JTextField checked;
  private JButton btnHelp;
  private JTextField name1;

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          logInChecker window = new logInChecker();
          window.frmWeek.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the application.
   */
  public logInChecker() {
    initialize();
  }

  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
    frmWeek = new JFrame();
    frmWeek.getContentPane().setBackground(new Color(51, 204, 153));
    frmWeek.setTitle("Week 5 - Interactive Assignment");
    frmWeek.setBounds(100, 100, 450, 300);
    frmWeek.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmWeek.getContentPane().setLayout(null);

    JLabel lblEnterYourUser = new JLabel("Enter Your User Name:");
    lblEnterYourUser.setFont(new Font("Tahoma", Font.BOLD, 11));
    lblEnterYourUser.setBounds(28, 36, 171, 31);
    frmWeek.getContentPane().add(lblEnterYourUser);

    name1 = new JTextField();
    name1.setColumns(10);
    name1.setBounds(28, 68, 179, 33);
    frmWeek.getContentPane().add(name1);

    JLabel lblEnterYourPassword = new JLabel("Enter Your Password:");
    lblEnterYourPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
    lblEnterYourPassword.setBounds(28, 112, 179, 14);
    frmWeek.getContentPane().add(lblEnterYourPassword);

    pass1 = new JTextField();
    pass1.setBounds(28, 137, 179, 31);
    frmWeek.getContentPane().add(pass1);
    pass1.setColumns(10);

    JButton btnCheck = new JButton("Attempt Login");
    btnCheck.setBackground(new Color(204, 102, 153));
    btnCheck.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnCheck.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        //define what we are looking for

        // get the values entered by the user
        String str1 = name1.getText();
        String str2 = pass1.getText();
        //create my user info strings
        String[] userNameArray = {
          "Vale.Vicky",
          "Lane.Lois",
          "Kent.Clark",
          "Wayne.Bruce",
          "Parker.Peter",
          "Rogers.Steve",
          "Luther.Lex",
          "Osborn.Harry",
          "Prince.Diana",
          "Admin"
        };
        String[] passwordArray = {
          "BatmanFan1",
          "SuperBoyFan1",
          "Kryptonite1",
          "Batman1",
          "SpiderMan1",
          "CtAmerica1",
          "Letitia1",
          "NewGoblin1",
          "WonderWoman1",
          "Password1"
        };

        boolean check = true;

        //Test for matching user name
        while (true) {
          for (String i: userNameArray) {
            if (str1.equals(i)) {
              checked.setText("Valid Login");
              check = false;
              break;
            }
          }
          //Test for matching password
          for (String j: passwordArray) {
            if (str2.equals(j)) {
              checked.setText("Valid Login");
              check = false;
              break;
            }
          }
          //return if both false
          if (check) {
            checked.setText("Invalid Login, Try Again");
            continue;
          }
          break;
        }
      }
    });
    btnCheck.setBounds(238, 136, 171, 32);
    frmWeek.getContentPane().add(btnCheck);

    JLabel lblYesOrNo = new JLabel("Checked Result:");
    lblYesOrNo.setFont(new Font("Tahoma", Font.BOLD, 11));
    lblYesOrNo.setBounds(189, 194, 113, 14);
    frmWeek.getContentPane().add(lblYesOrNo);

    checked = new JTextField();
    checked.setBounds(135, 219, 188, 31);
    frmWeek.getContentPane().add(checked);
    checked.setColumns(10);

  }
}

1 个答案:

答案 0 :(得分:0)

因此,根据我所看到的情况,userNameArraypasswordArray之间存在直接关联,这意味着nuserNameArray的值对应于n

passwordArray的值

这意味着您可以将整个搜索归结为单个if声明...

for (int index = 0; index < userNameArray.length; index++) {
    if (str1.equals(userNameArray[index]) && str2.equals(passwordArray[index])) {
        return true;
    }
}
return false

现在,已经说过,如果你有一些普通的旧Java对象(PO​​JO)封装了用户名和密码,然后它可以提供验证功能,那就简单多了

相关问题