hey everyone,
well i have made a method that would return array of string. the strings would be obtained from the file(employee.txt) every 4th line in the file contains employees loginID i need to store those ids in to one array and then return it.
here is the code of what i did so far

public String[] displyName(){
     int    num = 0;
     int idLine =2;
     String str="";
     String[] loginID=new String[20];

     

    try
    { BufferedReader bufeinp = new BufferedReader( new FileReader("employee.txt"));


      while ((str = bufeinp.readLine()) !=null)
      {
          if(num == idLine)
          {   int i =0;
              i++;
             
              loginID[i] =str;
              idLine=idLine+4;
  
          }
	++num;
      }
      bufeinp.close();
    }
    catch(IOException e)
    { System.err.println("Exception: " + e.getMessage());
      System.exit(1);
    }

     return loginID;
     
}

the problem is when i call the method im unable to retrieve any data stored in array from this method...
this is how i tried to call the values from displyName() method but this gave me null returns

String[] array = displyName();
             for(int i= 0; i<array.lenght; i++){
             System.out.println(array[0]);
             }

how would i be able to call this method and use loginID array ?
is there anything i am doing wrong in this method?


any suggestions would be greatly appreciated?
thank you

1. you misspelled array.length in your for loop
2. in your print statement, use the variable i instead of the constant 0

System.out.println(array[i]);

Also, in your displayName method:
1. Move the declaration of int i = 0; outside of the while loop (before line 13)
2. Move i++; to after the array assignment statement on line 19

thanks a lot for your reply i have fixed these mistakes but still the problem persist.

my output only return the last loginID from the employee.txt file and it is followed by null
here is the output:

login: john123
null
null
null.....

the problem is that there are 3 more loginID before john123 and the program does not display them
after i call it

String[] array = displyName();
             for(int i =0; i <array.length; i++){
             System.out.println(array[i]);
             }

thanks for your help

i just sorted it.
i just changed the place for my i variable in line 16 to line 2 and it worked like you told me
thank you for the help
thanks

i actually have one more question
well now i need to compare one string from JTextField with a value in the array and see whether there are values that are the same in the array

for(int i=0; i <array.length; i++){
                 if(loginText==array[i]){
                     found = true;}
             }
                     if(found)
                     {
                         System.out.println("same");
                     }

i wrote this code but i gives me no output what so ever

1. If you want to make sure you get output from an if statement, always put an else.
2. Use .equals to compare 2 strings

thanks again for your responce, well i tried this

for(int i=0; i <array.length; i++){
                 if(loginText.equals(array[i])){
                     found = true;
                 }
                 else
                 {
                    found= false;
                 }
             }
             
             if(found)
             {
                 System.out.println("same");
             }
             else{
                 System.out.println("not same");
             }

but it keeps on saying that the the values are not the same even if they are....

thanks for all your help, i really appreciated

What is the data type of loginText ? Is it a JTextField or a String? Assuming it is a String, try printing it out so you can see its value.

loginText is a String

String loginText = enterLogin.getText();

enterLogin is a JTextField

thank you

OK. Try printing it out either just before or just after the for loop you posted earlier.

gedas, I just saw something else you can try. Put a break; statement after setting found to true on line 3.

I just tried that and it gives me the value that i type in and the value it should look for in the array. for example i type in john123 it prints john123 and i am sure that john123 is in the text file from where the array is created.

i am starting to think that my display() method is somehow wrong but i do not see how:

here is the full class
these are the methods that this class containin the order:
public void registerGUI()
public String[] displyName(){
public void recordName(String name, String surname, String login, String password){
public void actionPerformed(ActionEvent e) {

package banksystem;

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;

public class Register implements ActionListener{// extends Login{

 
private JFrame registerFrame = new JFrame("Register");

private JTextField enterName = new JTextField(10);
private JTextField enterSurname = new JTextField(10);
private JTextField enterLogin = new JTextField(10);
private JPasswordField enterPassword = new JPasswordField(10);

private JLabel nameLabel = new JLabel("Enter your Name");
private JLabel surnameLabel =new JLabel("Enter your Surname");
private JLabel loginLabel = new JLabel("enter desired id");
private JLabel passwordLabel =new JLabel("enter desired password");



private JButton logIn =new JButton("Back To Login");
private JButton saveUser = new JButton("Save User") ;

private JPanel buttonPanel = new JPanel();
private JPanel namePanel =new JPanel(new GridLayout(8,1));
private JPanel registerPanel = new JPanel(new BorderLayout());


   public void registerGUI(){
        registerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        registerFrame.setSize(700,400);
        registerFrame.setResizable(true);
        registerFrame.getContentPane().add(registerPanel, BorderLayout.CENTER);

        buttonPanel.add(logIn);
        buttonPanel.add(saveUser);
        logIn.addActionListener(this);
        saveUser.addActionListener(this);
         
        namePanel.add(nameLabel);
        namePanel.add(enterName);
        enterName.addActionListener(this);

        namePanel.add(surnameLabel);
        namePanel.add(enterSurname);
        enterSurname.addActionListener(this);

        namePanel.add(loginLabel);
        namePanel.add(enterLogin);
        enterLogin.addActionListener(this);

          namePanel.add(passwordLabel);
        namePanel.add(enterPassword);
        enterPassword.addActionListener(this);




        registerPanel.add(namePanel);
        registerPanel.add(buttonPanel,BorderLayout.SOUTH);


        registerFrame.setVisible(true);

    }

public String[] displyName(){
     int    num = 0;
     int i =0;
     int lvl =2;
     String str="";
     String[] loginID=new String[20];

     

    try
    { BufferedReader bufeinp = new BufferedReader( new FileReader("employee.txt"));


      while ((str = bufeinp.readLine()) !=null)
      {
          if(num == lvl)
          {                           
              loginID[i] =str;
              i++;
              lvl=lvl+4;
               
          }
	++num;
      }
      bufeinp.close();
    }
    catch(IOException e)
    { System.err.println("Exception: " + e.getMessage());
      System.exit(1);
    }

     return loginID;
     
}

public void recordName(String name, String surname, String login, String password){
             try
            {
                BufferedWriter bufewr;
                bufewr = new BufferedWriter(new FileWriter("employee.txt",true));
                for (int i=0; i<1; i++){
                   bufewr.write(name);
                    bufewr.newLine();
                    bufewr.write(surname);
                    bufewr.newLine();
                    bufewr.write(login);
                    bufewr.newLine();
                    bufewr.write(password);
                    bufewr.newLine();

                   
                }

             bufewr.flush();
             bufewr.close();
            }
            catch(IOException evt)
            {
                System.err.println("Exception: " + evt.getMessage());
                System.exit(1);
            }




}
 
    public void actionPerformed(ActionEvent e) {
         String nameText = enterName.getText();
         String surnameText = enterSurname.getText();
         String loginText = enterLogin.getText();
         String passwordText = enterPassword.getText();
         boolean found= false;
         

        if(e.getSource() == logIn){
            System.out.println("you have clicked back to login");
            registerFrame.setVisible(false);
            new Login();
            
        }
         if(e.getSource() == saveUser){
             String[] array = displyName();

               if(nameText.isEmpty() || surnameText.isEmpty() || loginText.isEmpty() || passwordText.isEmpty()){
                 System.out.println("some fields maybe empty");
                 }


             for(int i=0; i <array.length; i++){
                 if(loginText.equals(array[i])){
                     found = true;
                 }
                 else
                 {
                    found= false;
                 }
             }

             if(found)
             {
                 System.out.println("same");
             }
             if(!found){
                 System.out.println("not same");
             }


System.out.println(loginText+"  login text");

                recordName(nameText,surnameText,loginText,passwordText);
//
                System.out.println("you have clicked save user");
  
        
           

                        
                

            
        }


        
    }


}

OK. Try putting a break; statement after line 164.

If that doesn't fix the problem, then try printing out the elements in your array to make sure the value you are expecting is really in the array.

wow it works break; works its showing me when there are same values im not sure i understand why it works
but thank you very much you been lots and lots of help
thank you

Just so you understand, say you have 10 items in your loop, and you find a match on the 5th item. You will set found to true, but then when you check item #6 you will set found back to false because #6 doesn't match. When you put a break after setting found to true, that means get out of the loop so nothing else will set found back to false.

thank you that does actually make seance. you have been a lot of help for me i do really appreciated.
it would of taking me days to work this out, and many hours googling.
thank you once again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.