我是一名試圖完成在線課程任務的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);
}
}
爲什麼不使用任何類型的用戶對象來封裝用戶名和密碼? – Makoto
你爲什麼要做一個「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
因此...搜索用戶列表,爲每個匹配從密碼列表中找到相應的密碼,並比較,如果它們是相同的,那麼很好,如果不是,繼續下去。實質上,您只需要搜索一個列表 – MadProgrammer