3

I am new to programming and I decided to learn Java. I had just finished reading about one dimensional array and I am having trouble with searching.

The summary of this program I had made is to ask the user how many students will be enrolled in the class. The user then inputs the name of the students based on the length of the array. Then I want the to be able to have the user search for the students name. How can i accomplish this? What I want to accomplish is when the user inputs the first name it will return the list of full names that has the matching first name. I really struggling with this. Please don't give any advanced methods. I would like to stay in pace pace with my book.

I am using introduction to java programming comprehensive version 10th edition.

import java.util.Scanner;

public class classSystem    {

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Weclome instructure to your Class System!");
        System.out.println("Follow each steps to turn in your work instructor.");
        System.out.println("\n1.) Enroll Students:");

        System.out.print("\nHow many students are enrolled? ");
        int studentAmount = input.nextInt();

        String[] enrolledStudents = getStudentAttendance(studentAmount);

        System.out.println("Here is your attendance list:");

        for (int count = 0; count < enrolledStudents.length; count++) {
            System.out.print("\n\t" + (count + 1) +     ".) " + enrolledStudents[count]);
        }

        System.out.print("\n\nWhat sudent do you want to search: ");

        String studentSearch = input.nextLine();

        System.out.println(getStudent(enrolledStudents, studentSearch));
    }

    public static String[] getStudentAttendance(int studentAmount)
    {
        Scanner input = new Scanner(System.in);

        String[] enrolledStudents = new String[studentAmount];

        System.out.println("Input the students names:");
        for (int count = 0; count < enrolledStudents.length; count++)
        {
            System.out.print((count + 1) + ".) ");
            enrolledStudents[count] = input.nextLine();
        }

        return enrolledStudents;
    }

    public static String getStudent(String[] enrolledStudents, String StudentSearch)
    {
        for (int count = 0; count < enrolledStudents.length; count++)
        {
            if(StudentSearch.equals(enrolledStudents[count])) 
            {
                return getStudent;
            }
        }
    }

}
  • 1
    Advice: indentation is very important. Look at the examples in your book, you'll see that the content of every `if` and `for` and any other block of code between `{` is indented one level. Also, there are conventions for names: variable and method names are lower camelCase (which you did well), but class, interface and enum names are upper CamelCase. – RealSkeptic Jan 18 '17 at 18:52

2 Answers2

1

I have updated your code. Please see the comments inline. Hope this helps.

import java.util.Scanner;

class classSystem {
    static Scanner input; //created a static reference for Scanner
                          //as you will be using in both the methods

    public static void main(String[] args) {
        input = new Scanner(System.in);   //creating the Scanner object.
        System.out.println("Weclome instructure to your Class System!");
        System.out.println("Follow each steps to turn in your work instructor.");
        System.out.println("\n1.) Enroll Students:");

        System.out.print("\nHow many students are enrolled? ");
        int studentAmount = input.nextInt();
        input.nextLine();     //added this to consume new-line leftover

        String[] enrolledStudents = getStudentAttendance(studentAmount);

        System.out.println("Here is your attendance list:");

        for (int count = 0; count < enrolledStudents.length; count++) {
            System.out.print("\n\t" + (count + 1) + ".) " + enrolledStudents[count]);
        }

        System.out.print("\n\nWhat sudent do you want to search: ");

        String studentSearch = input.nextLine();   

        System.out.println(getStudent(enrolledStudents, studentSearch));
        input.close();   //close the scanner 
    }

    public static String[] getStudentAttendance(int studentAmount) {

        String[] enrolledStudents = new String[studentAmount];

        System.out.println("Input the students names:");
        for (int count = 0; count < enrolledStudents.length; count++) {
            System.out.print((count + 1) + ".) ");
            enrolledStudents[count] = input.nextLine();
        }

        return enrolledStudents;
    }

    public static String getStudent(String[] enrolledStudents, String studentSearch) {
        boolean flag = false;   //added flag, this will be true if name is found 
                                //otherwise false
        for (int count = 0; count < enrolledStudents.length; count++) {
            if (studentSearch.equals(enrolledStudents[count])) {
                flag = true;
                break;          //if name is found breaking the loop.
            } else {
                flag = false;
            }
        }
        if (flag == true)     //checking the flag here
            return studentSearch + " is present in the class";
        else
            return studentSearch + " is not present in the class: ";
    }

}

I am getting below result after running my code.

enter image description here

Maninder Singh
  • 182
  • 1
  • 9
  • This is what i get [Ljava.lang.String;@5146213 all the time –  Jan 19 '17 at 02:16
  • Do you get this error when you run my code ? What line you are getting the error – Maninder Singh Jan 19 '17 at 05:08
  • Yes i get the error running your code. My ide doesn't show what line. But that error i commented shows after i enter an input when im asked to search a student. –  Jan 19 '17 at 06:17
  • I have attached the screenshot of what I am getting after running my code. – Maninder Singh Jan 19 '17 at 18:49
  • I had fixed it it was my poor syntax. I just have a question why there is a input before the main method. And what does the .close (); method do for the input. Thats very new to me. –  Jan 19 '17 at 19:35
  • As you are using scanner object in both the methods, so i have declared it static so that you can use it in both methods. – Maninder Singh Jan 21 '17 at 01:47
  • And the .close is for closes the scanner. According to Orcale Java docs -->If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect. So the underlying stream here is "System.in" which will be closed. And you cannot use this stream later in your program. So thats why i have closed it at the end of program. – Maninder Singh Jan 21 '17 at 01:48
  • This is a good practice to close all the resources which you have utilized when you don't need them. – Maninder Singh Jan 21 '17 at 01:49
  • For more info check out this link http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close() – Maninder Singh Jan 21 '17 at 01:50
0

Looks like you already got the idea how to search using .equals() method. Assuming you'll fix getStudent() method by handling "not found" situation, you should be done.

Next, do you want to improve your search, is that your real question? That depends on what type of search do you want to implement. Partial name match, name starts with, ignoring upper/lower case, wildcard search are different options. If that is what you want, please add it to the question.

Alexander
  • 609
  • 6
  • 19