我想要做的是每当我使用特定ID登录时,它将根据键入的ID拉取学校名称。例如我在我的文件testing.txt中有这个并且我登录到0114343并且想验证如果学校是“通信学院”,它会将标签更改为“SOC”
这是我的代码到目前为止,但它不起作用它将只显示SOC,无论我登录的是哪个ID。感谢您的任何帮助。
LoginClass
public class TestingProgram {
JButton button1,button2;
JLabel label;
JTextField idField,passwdField;
BufferedWriter toFile;
BufferedReader validation;
String valuesArray[]={};
String idTitle;
int exists=0;
public TestingProgram()
{
JFrame frame = new JFrame();
JPanel pnl = new JPanel();
//Buttons
button1 = new JButton("Add");
button2 = new JButton("Login");
//TextField
idField = new JTextField(15);
passwdField = new JTextField(15);
pnl.add(button1);
pnl.add(button2);
pnl.add(idField);
pnl.add(passwdField);
button2.addActionListener((ActionEvent e )->{
Validation();
});
frame.add(pnl,BorderLayout.NORTH);
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public String getIdTitle()
{
idTitle = idField.getText();
return idTitle;
}
public void Validation()
{
try{
validation = new BufferedReader(new FileReader("testing.txt"));
String values=null;
while((values = validation.readLine())!=null)
{
valuesArray = values.split(",");
if(idField.getText().equals(valuesArray[0]) && passwdField.getText().equals(valuesArray[1]))
{
exists = 1;
break;
}
}
if(exists==1)
{
SecondTest sec = new SecondTest();
sec.second();
sec.frame.setTitle(getIdTitle());
}
else
{
System.out.println("Does not exsits in file");
}
}
catch(FileNotFoundException ex){
ex.printStackTrace();
}
catch(IOException ex){
ex.printStackTrace();
}
}
public static void main(String[] args)
{
new TestingProgram();
}
}
第二课
public class SecondTest
{
JFrame frame;
JButton button;
JLabel label;
JPanel panel;
BufferedReader validation;
String valuesArray[] = {};
String school;
public void second()
{
frame = new JFrame();
button = new JButton("Button");
label = new JLabel("");
panel = new JPanel();
panel.add(button);
panel.add(label);
try{
validation = new BufferedReader(new FileReader("testing.txt"));
String values=null;
while((values=validation.readLine())!=null)
{
valuesArray=values.split(",");
if(valuesArray[2].equals("SchoolofCommunication"))
{
school = "SOC";
break;
}
else if(valuesArray[2].equals("SchoolofEngineering"))
{
school = "SOE";
break;
}
}
if(school.equals("SOC"))
{
label.setText("SOC");
}
else
{
label.setText("SOE");
}
}
catch(FileNotFoundException ex){
ex.printStackTrace();
}
catch(IOException ex){
ex.printStackTrace();
}
frame.add(panel,BorderLayout.CENTER);
frame.setSize(450,450);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
答案 0 :(得分:0)
second()
没有ID上下文。 while循环只读取文件,SOC恰好是文件中的第一个条目,因此它会立即中断。
while((values=validation.readLine())!=null) {
valuesArray=values.split(",");
if(valuesArray[2].equals("SchoolofCommunication")) {
school = "SOC";
break;
}
// this else branch is not reachable based on the contents of the file
else if(valuesArray[2].equals("SchoolofEngineering")) {
school = "SOE";
break;
}
}
所以你需要传递ID上下文:
second(String id)
并在while循环中使用它来查找文件中的相关行:
while((values=validation.readLine())!=null) {
valuesArray=values.split(",");
if(valuesArray[0].equals(id) && valuesArray[2].equals("SchoolofCommunication")) {
school = "SOC";
break;
} else if((valuesArray[0].equals(id) && valuesArray[2].equals("SchoolofEngineering")) {
school = "SOE";
break;
}
}