如何从树形图中获取某些数据

时间:2016-12-08 18:46:40

标签: java bluej

我有一个java程序,可以将学生,教职员工和教师存储在一个目录中。我可以让程序打印目录中的所有数据,但我一直无法打印学生。任何人都可以请你告诉我哪种方法来解决这个问题。这让我疯了。

以下是从我的目录中获取所有数据的代码:

 /**
 * Print the list using an iterator.
 */
private void printList()
{   
    // Retrieve data from the TreeMap
    Set directorySet = directory.entrySet();

    // Get an iterator
    Iterator iterator = directorySet.iterator();

    // Display elements
    System.out.println("Iterate through the TreeMap and print its contents \n");

    // Warning if TreeMap is empty. 
    if(directory.isEmpty()){
        System.out.println("The directory is empty, nothing to show. Returning to main menu");

        MainMenu();
    } else 
    {
        while(iterator.hasNext()) {
            Map.Entry directory = (Map.Entry)iterator.next();
            System.out.print(directory.getKey() + ": ");
            System.out.println(directory.getValue());
        }
        System.out.println("\n");
        System.out.println("Reached end of list");

        continueMenu();
    }
}

我需要做的就是打印目录中学生的所有名字。

这是输入学生信息的代码部分。

 public void newPerson()
{
    // Input 
    String userInputTwo;

    // Scanner for user input.
    Scanner in = new Scanner(System.in);
    Scanner userInput = new Scanner(System.in); 
    System.out.println("\n");

    // Add new user data.
    System.out.println("Enter the details of person you would like to add. \n");
    System.out.println("What is the last name?");
    lastname = userInput.next();
    System.out.println("\n");
    System.out.println("What is the first name?");
    firstname = userInput.next();
    System.out.println("\n");
    System.out.println("What is the Email?");
    Email = userInput.next();

    while(keepLoopingTypeOfPerson==true)
    {
        // Add new user data.
        System.out.println("\n");
        System.out.println("Select the type of person: student, staff, faculty");
        String typeOfPerson = in.nextLine();
        switch(typeOfPerson)
        {
            case "student":
            String studentStatus = Students.GetStudentStatus();
            directory.put(lastname +" "+ firstname, " Email - " + Email 
                + ", Class Status - " + studentStatus);
            System.out.println("\n");
            System.out.println("Person added \n");
            System.out.println("Add another person? y or n \n");
            userInputTwo = in.nextLine().trim().toLowerCase();

            if (userInputTwo.equals("y")) 
            {
                newPerson();
            } else if (userInputTwo.equals("n"))

            // return to menu
               keepLoopingNewPerson=true;

0 个答案:

没有答案