-2

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;

      }
   }
Aswathi
  • 1
  • 3
  • 1
    it seems to me you're reading the file two lines at a time (via nextLine()) instead of reading one line and splitting it at the space. So you're comparing one field with the full line, which won't work. – JP Moresmau Jun 10 '15 at 14:19
  • Can we have sample output or the stacktrace if there is an error? – basic Jun 10 '15 at 14:21
  • I have tried comparing with single lines ie, without making the data as two fields by space, just made it as a word and tried the same. Still its not working. – Aswathi Jun 10 '15 at 14:22
  • @JPMoresmau is correct. If I run your program and enter Excgemem = `KFHK ERTF`, ClrMem = `ABCD XDFG`, I see "REquest Succesfl". A good idea when trying to solve these sorts of problems is to *print the values* before comparing them. That will let you see exactly what the program is trying to do. – azurefrog Jun 10 '15 at 14:23
  • Try to use `scan.next()` instead of `scan.nextLine()` and see if its working. – Codebender Jun 10 '15 at 14:24
  • @azurefrog : I have tried in that way and I can see the Output as successful , but why the same result is not coming when we give the data in the order what is there in the text file. ie, ABCD XDFG as first field and KFHK ERTF as second field? – Aswathi Jun 10 '15 at 14:30
  • 1
    @Aswathi, that is probably because your labels are pointing to opposite text fields... `Excgemem` is pointing to `jTextField1` and `ClrMem` is pointing to `jTextField` – Codebender Jun 10 '15 at 14:43

1 Answers1

2

I recommend you to use an ArrayList to get all the lines of your file and then split them to get EGM and CM values. Like this:

Scanner s = new Scanner(new File(//Here the path of your file));

ArrayList<String> list = new ArrayList<String>();

while (s.hasNext())
{
    list.add(s.nextLine());
}

Now you have all the lines of your file, so you can split them to get both values, like this:

for(int i = 0; i < list.size(); i++)
{
   String[] line  = list.get(i).split(" ");

   EGM = line[0];
   CM = line[1];

}

Now you can compare both values:

if (exmem.equals(EGM)&& clermem.equals(CM))
{
    JOptionPane.showMessageDialog(null,"REquestSuccesfl");
}
else 
{
    JOptionPane.showMessageDialog(null,"Wrong exmem/clermem");
}

Finally, close your Scanner variable, like this:

s.close();

I expect it will be helpful for you!

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • @Aswathi Look at this for this error: http://stackoverflow.com/questions/21228284/exception-in-thread-awt-eventqueue-0-java-lang-nullpointerexception-error. It seems that it's because you didn't initialize something relation with your EventQueue import. Please check it. – Francisco Romero Jun 10 '15 at 15:14
  • I have tried initializing everything, but still i can see these errors. – Aswathi Jun 10 '15 at 16:34
  • @Aswathi Can you edit your question with the code that you have changed please? I think it will be easier to try to fix the bug. – Francisco Romero Jun 10 '15 at 22:18
  • QError404: I have edited the code . Please see the question, and hellp me. – Aswathi Jun 11 '15 at 07:53
  • @Aswathi beware with spaces in `File ("/home/achu/workspace/GUI/src/logincode.csv")`. Also, have you tried with the full path of your file? – Francisco Romero Jun 11 '15 at 09:20
  • That one is a editing mistake here, in stack overflow. There is no space in the code there is no space like that. And I have mentioned the complete file path. – Aswathi Jun 11 '15 at 10:33
  • @Aswathi and what it's the error that it gives to you? the same error? – Francisco Romero Jun 11 '15 at 10:38
  • @Aswathi Why do you have your variable Object `exmem` if you don't use it in the code? It is not initialize. It's why equals method won't work. My mistake sorry. `.equals()` method have to use Strings. So, where are the values of your login? I can't clarify myself with the names of your variables, sorry. – Francisco Romero Jun 11 '15 at 12:26
  • exmem (and clermem) is the variable for the value entering by user. EGM and CM are the corresponding variale values to be compared in the txt file. – Aswathi Jun 11 '15 at 12:40
  • @Aswathi Ok then I understood it properly but if you see your code exmem variable doesn't have any value. The same with clermem. – Francisco Romero Jun 11 '15 at 12:55
  • I have changed the same in the code , now i am not getting the error where as , I am getting only the 'wrong exmem/clrmem' popup even if am giving the data from text file. (i have tried giving exmem as ABCD and clrmem as XDFG, I got wrong data message. And I have tried giving ABCD CLRMEM as exmem and KFHK ERTF as clrmem, still same wrong message is coming. I edited the code in question, Please check the same – Aswathi Jun 12 '15 at 08:44
  • Hi, I have changed my code a little, and I am able to get the output, but now the problem is the successful message is comming only if I give ABCD as exmem and XDFGKFHK as clrmem. Can you tell me why the second line is getting added with the first line ? I have printed the o/p and the file is reading as ABCD XDFGKFHK ERTFFFSF JFKFSETG kgfb why It is reading like this? Please check the edited code in question . – Aswathi Jun 12 '15 at 12:18
  • @Aswathi Ok I'm going to see it in a few minutes let me prove it in my Eclipse please and I put here my answer. – Francisco Romero Jun 12 '15 at 12:59
  • @Aswathi where do you have your add method? In another class? What exactly does inside of it? – Francisco Romero Jun 12 '15 at 15:58
  • I have changed my script as mentioned below . – Aswathi Jun 23 '15 at 06:47
  • there is no other class. Its just adding the Jbutton BOK – Aswathi Jun 23 '15 at 08:46
  • Ho to add loop here for checking the user entered string in the entire file line by libe – Aswathi Jun 23 '15 at 08:56
  • @Aswathi It's what I put in my answer. It just through all the file and take line by line. – Francisco Romero Jun 23 '15 at 11:40