I am trying to make a GUI which works as a login screen. The code should compare the value entered with the values in a txt file. (two fields needed to be compared) The values in the text file are given as two columns separated by a space. My code is not comparing the data properly.
Login.txt file:
ABCD XDFG
KFHK ERTF
FFSF JFKF
SETG kgfb
Code part:
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
@SuppressWarnings({ "serial", "unused" })
public class Guilook extends JFrame{
public JTextField exmem;
public JTextField clermem;
public JButton bok;
private Object EGM;
private Object CM;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Guilook window = new Guilook();
window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Guilook() {
initialize();
}
public void initialize() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setSize(350,300);
clermem = new JTextField();
clermem.setBounds(90, 114, 105, 22);
add(clermem);
clermem.setColumns(20);
exmem = new JTextField();
exmem.setBounds(90, 79, 105, 22);
add(exmem);
exmem.setColumns(10);
JLabel lblExcmem = new JLabel("Exmem");
lblExcmem.setBounds(220, 82, 76, 16);
add(lblExcmem);
JLabel lblClrmem = new JLabel("clrmem");
lblClrmem.setBounds(220, 117, 66, 16);
add(lblClrmem);
JButton bok = new JButton("OK");
bok.setBounds(144, 158, 97, 25);
bok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
String info = ReadFile();
System.out.println(info);
String[] split = info.split(" ");
String EGM=split[0];
String CM =split[1];
Scanner s=null;
if(exmem.getText().equals(EGM) && clermem.getText().equals(CM)){
JOptionPane.showMessageDialog(null,"REquestSuccesfl");
}else{
JOptionPane.showMessageDialog(null,"Wrong exmem/clermem");
}
}});
add(bok);
}
private static String ReadFile(){
String line=null;
String text="";
FileReader filereader=null;
try{
filereader =new FileReader(new File ("/home/v3nky/Downloads/eclipse_java/EurexGUI/sample.txt"));
BufferedReader bf=new BufferedReader(filereader);
while((line=bf.readLine()) !=null){
text=text+line+'\n';
}
bf.close();
}catch(Exception e){
e.printStackTrace();
}
return text;
}
}