How to create a search method for an array

Nate_thp picture Nate_thp · Apr 16, 2015 · Viewed 11.9k times · Source

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

Answer

TheJavaCoder16 picture TheJavaCoder16 · Apr 17, 2015

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