2017-04-11 44 views
-3

我是一名试图完成在线课程任务的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); 

    } 
} 
+0

为什么不使用任何类型的用户对象来封装用户名和密码? – Makoto

+0

你为什么要做一个“while(true)'循环,一次执行?不要调用'setBounds(...)'并使用'null'布局。参见[null layout is evil](http://www.fredosaurus.com/notes-java/GUI/layouts/nulllayout.html)和[这个问题]中的答案(http://stackoverflow.com/questions/6592468/why-it-it-fr-on-to-use-a-null-layout-in-swing)了解更多信息。为什么用户将自己的密码写入“JTextField”而不是“JPasswordField”?你应该对模型和存储用户和密码在一个存储两个值的单个对象,然后通过此对象比较 – Frakcool

+0

因此...搜索用户列表,为每个匹配从密码列表中找到相应的密码,并比较,如果它们是相同的,那么很好,如果不是,继续下去。实质上,您只需要搜索一个列表 – MadProgrammer

回答

0

因此,基于我所看到的,有userNameArraypasswordArray之间有直接的关系,这意味着在价值nuserNameArray对应于值在npasswordArray

这意味着你的整个搜索可以归结到一个单一的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)的其封装了用户名和密码,然后可以提供验证功能

+0

谢谢(和其他人)对方向。我最终将字符串添加到hashmap中并实现了简化for语句的版本。 –