Java 基于登录id从文件中提取数据
我试图做的是,每当我使用特定id登录时,它都会根据键入的id提取学校名称。例如,我的文件中有这个,我登录到0114343,并希望验证如果学校是“通信学校”,它是否会将标签更改为“SOC” 这是我到目前为止的代码,但它不起作用,无论我登录哪个id,它都只显示SOC。任何帮助都将不胜感激,谢谢 登录类Java 基于登录id从文件中提取数据,java,Java,我试图做的是,每当我使用特定id登录时,它都会根据键入的id提取学校名称。例如,我的文件中有这个,我登录到0114343,并希望验证如果学校是“通信学校”,它是否会将标签更改为“SOC” 这是我到目前为止的代码,但它不起作用,无论我登录哪个id,它都只显示SOC。任何帮助都将不胜感激,谢谢 登录类 public class TestingProgram { JButton button1,button2; JLabel label; JTextField idField,passwdField
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);
}
二等舱
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);
}
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;
}
}