How to create a search method for an array

12,962

Solution 1

You never populated the Student students[] array... You retrieved the values you would populate them with here:

while(in.hasNextLine())
    {
        String SName = in.next();
        String DoB = in.next();
        String Gender = in.next();
        String Address = in.next();

    }

But you never actually set those values into a Student object in the students[] array

Do something like this:

int i = 0;

while(in.hasNextLine())
    {
        String name = in.next();
        String dateOfBirth = in.next();
        String gender = in.next();
        String address = in.next();

        students[i] = new Student(name, dateOfBirth, gender, address);
        i++
    }

Also, you might consider ditching the array and using some sort of List or Hash object... If your file contains more than 20 lines, the array will be out of index when you try to define the 21st value.. With an arraylist or a List you wouldn't have that problem

Solution 2

I took a liberty to tweak your code as previous answer mentioned, it's better to use array list in your case. You could make a small student container class within your reader. The get name method is also kinda redundant ;s

package test;

import java.io.*;
import java.util.*;

public class readStudents{

    ArrayList<Student> students = new ArrayList<Student>();

    class Student {

        private String name;
        private String dob;
        private String gender;
        private String address;

        public Student(String name, String dob, String gender, String address) {
            this.name = name;
            this.dob = dob;
            this.gender = gender;
            this.address = address;
        }

        public void fillStudentArray() {
            // properties

            int size; // total number of Students in collection

            File file = new File("StudentDetails.txt");

            try {
                Scanner in = new Scanner(file);

                while (in.hasNextLine()) {
                    String SName = in.next();
                    String DoB = in.next();
                    String Gender = in.next();
                    String Address = in.next();

                    students.add(new Student(SName, DoB, Gender, Address));
                }

            }

            catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }

        public String getName(Student student) {
            return student.name;
        }

        public void printname() {
            System.out.println("hello");
        }

        public Student search(String name) {
            System.out.print("Enter the name you wish to search: ");

            for (Student student : students) {
                if (student.name.equalsIgnoreCase(name))
                    ;
                return student;
            }

            return null;
        }
    }
}
Share:
12,962
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    My code is as follows:

    import java.io.*;
    import java.util.*;
    
    public class readStudents extends Object
    { 
        private String SName = "";
        private String DoB = "";
        private String Gender = "";
        private String Address = "";
        Student [] students = new Student[20];
    
    
        public void fillStudentArray()
        {
            // properties
    
            int size; // total number of Students in collection
    
            File file = new File("StudentDetails.txt");
    
        try
        {
            Scanner in  = new Scanner(file);
    
    
            while(in.hasNextLine())
            {
                String SName = in.next();
                String DoB = in.next();
                String Gender = in.next();
                String Address = in.next();
    
            }
    
        }
    
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
    
        }   
    
        public String getName()
        {
            return this.SName;
        } 
    
        public void printname()
        {
            System.out.println("hello");
        }
        public Student search(String name)
        {
            System.out.print("Enter the name you wish to search: ");
    
            for (int i = 0; i < this.students.length; i++)
            {
                Student s = this.students[i];
                if (s.getName().equalsIgnoreCase(name))
                {
                    return s;
                }
            }
            return null; 
        }
    }   //end class students
    

    However I am trying to create a well refined program that I can call on these methods from another main file with as minimal code as possible in that file.

    The search method at the bottom is tripping me up as I am assuming I need to put something to do with the array in my getName() method but I can't figure it out.

    Since I am doing this as a class for another main method, with the placement of my array initialization and declaration it allows the other methods to access it but it leaves me with no way to create this array from the main method unless I am missing something?

    This is the error jCreator is throwing:

    F:\University\Ass2\readStudents.java:62: error: cannot find symbol
                if (s.getName().equalsIgnoreCase(name))
    
                     ^
    symbol:   method getName()
    location: variable s of type Student