-1

Thank you for taking the time to look at this. I am having some trouble with a project of mine. As of now I have 2 array lists that collect names and ids. I want to link the objects of both lists (the name and id) with each other so that I can perform a insertion sort algorithm later.

Tasks:
1) Ask the user to input names and IDs for team members. Users should alternate inputting names and IDs on separate lines, as shown in the sample run below. As the information is provided, TeamMembers will be added to an ArrayList of TeamMember objects. Remember that names should be stored in title case inside of the TeamMember class.

2)The user should enter the word "STOP" in any combination of lowercase and uppercase letters to stop entering team member information.

3)Sort the ArrayList in increasing order by ID using the insertion sort algorithm. You can choose to use the insertion sort algorithm as you are inserting each new TeamMember object into the array, or you can wait until the entire array is constructed before sorting all of its members at once.

4)After all the names are entered and sorted, print the contents of the ArrayList using ArrayList.toString().

package com.company;

import java.util.*;
import java.util.HashMap;

public class Main {

public static void main(String[] args) {
    String inputName;
    String inputID;

    List<String> fullName = new ArrayList<String>();
    List<String> idString = new ArrayList<String>();

    while (!(fullName.contains("stop"))) {
        Scanner uInput = new Scanner(System.in);
        //String checkInput = "test";
        System.out.println("Please enter the name: ");
        inputName = uInput.nextLine();

        fullName.add(inputName.toLowerCase());

        // System.out.println("List: " + fullName);  //Check what is in the fullName List
        // System.out.println("List Size: " + fullName.size()); // Check the size of fullName ArrayList


        System.out.println("Please enter the ID: ");
        inputID = uInput.nextLine();

        idString.add(inputID.toLowerCase());
    }
    fullName.remove(fullName.size() - 1);  //removing the last word, which is always "stop"
    idString.remove(idString.size() - 1);    //(Same as above)

    //insertionSort(fullName);
    //insertionSort(idString);
}

HashMap newmap = new HashMap(); //creating hash map

Best, Nate

  • What do you mean `link the objects`. Can you show an example? – Thiyagu Apr 30 '18 at 18:31
  • @user7 I have updated the post –  Apr 30 '18 at 18:34
  • 2
    I couldn't find anything new in your post.. – Thiyagu Apr 30 '18 at 18:35
  • @user7 I added a todo list. In short, I want to link the name and id that are input by the user to each other. –  Apr 30 '18 at 18:39
  • This looks like a homework assignment and perhaps you did not fully understand the instructions. The loop which takes in user information looks reasonable. Before you sort anything, however, you need to associate the elements within each list as they were given by the user. For example if Fred has ID 456 and Jane as ID 987 this association might need to be created prior to any sort operation. I think once decide how to create this association the problem will make more sense. How do you think you might do this? – natersoz Apr 30 '18 at 18:51

1 Answers1

1

As the information is provided, TeamMembers will be added to an ArrayList of TeamMember objects

So, you need to have a TeamMember class that has the names and ids

class TeamMember {
    private String name;
    private String id;

    public TeamMember(String name, String id) {
        this.name = name;
        this.id = id;
    }
}

Remember that names should be stored in title case inside of the TeamMember class.

Refer to this post for converting a String to title case

Clues for rest of your work:

The user should enter the word "STOP" in any combination of lowercase and uppercase letters to stop entering team member information.

After getting the name, use String's equalsIgnoreCase to check if the entered String is a variation of STOP.

Sort the ArrayList in increasing order by ID using the insertion sort algorithm.

Either you must pass a Comparator (to the sorting method) or make TeamMember implement Comparable to compare two TeamMember objects

Thiyagu
  • 17,362
  • 5
  • 42
  • 79