0

I am making a while loop that asks for a name and an id and adds it to an ArrayList repeatedly until told to stop for my computer science class. How could I get the name of the new "TeamMember" to change every time? Also, would that even work and if not why and how could I get it to work? This is my code (Main):

import java.util.ArrayList;
import java.util.Scanner;

public class TeamClassMain {

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    ArrayList <TeamMember> list = new ArrayList();
    String name;
    String id;
    String ArrayVar;
    int number = 0;
    int stop = 0;
    while (stop == 0) {
        System.out.println("Enter the next name:");
        name = scan.nextLine();
        name = name.toLowerCase();
        if (name == "stop") {
            stop = 1;
        }
        System.out.println("Enter the next ID:");
        id = scan.nextLine();
        ArrayVar = "member " + number;
        TeamMember member1 = new TeamMember(name, id);
        number++;
        }
   }  
}

This isn't necessary, but it might be interesting to know what TeamMember is so this is my TeamMember class:

import java.lang.*;

public class TeamMember {

String fullName;
String idString;

TeamMember(String name, String id) {
    name = name.toUpperCase();
    char character;
    String string;
    int num;
    for (int i = 0; i < name.length() - 1; i++) {
        if (i == 0) {
             character = name.charAt(0);
             string = "" + character;
             string = string.toUpperCase();
             character = string.charAt(0); 
             i = character;
        }
        if (name.charAt(i) == ' ') {
            character = name.charAt(i + 1);
            string = "" + character;
            string = string.toUpperCase();
            character = string.charAt(i + 1);
            name.substring(i + 1, character);
        }
    }
    fullName = name;
    idString.valueOf(id);
}

public String toString() {
    return fullName;    
}

int compareTo(TeamMember other) {
    return idString.compareTo(other.idString);
    }
}
Bentaye
  • 9,403
  • 5
  • 32
  • 45
Aidan
  • 89
  • 1
  • 2
  • 11
  • See: [How do I compare strings in Java?](https://stackoverflow.com/q/513832/5221149) – Andreas Apr 13 '18 at 16:24
  • you never actually add your TeamMember to your arraylist called list – RAZ_Muh_Taz Apr 13 '18 at 16:26
  • You do create new `TeamMember` with the `name` you got from the input. So, what's the problem? I fail to understand what are you asking. – lexicore Apr 13 '18 at 16:26
  • What is purpose of `ArrayVar`? You never use it. --- What is logic in `TeamMember` constructor supposed to do? `name.substring(i + 1, character)` does nothing, since you never assign returned values anywhere. And `i = character`? Do you understand what assign `char` value to `int` value does? – Andreas Apr 13 '18 at 16:27
  • What do you think is the "name of the teammember"? – Turing85 Apr 13 '18 at 16:28
  • @Turing85 member1. I now understand that what I asked is irrelevant – Aidan Apr 13 '18 at 16:33

2 Answers2

0

you need to add TeamMember to ArrayList

TeamMember member1 = new TeamMember(name, id);
list.add(member1);//add to list

you should break the while loop after stop that stop not gonna add in list and use equal for comparing the string

if (name.equals("stop")) {
    stop = 1;
    break;
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
Roushan
  • 4,074
  • 3
  • 21
  • 38
0

Step 1 - Don't use a hammer to drive screws. Specifically, pay attention to the input data: you are asking the user to enter a map value between a name and an id. It appears that the name is the key and the id is the value. Store map "stuff" in a Map.

Step 2 - The TeamMember class provides just about 0 value. Just use the map between name and id.

In a less toy problem, TeamMember would do more that map between name and id. In that case, determine which is the appropriate key (id or name) and allocate (use new) a new TeamMember as the value each time you insert into the Map.

DwB
  • 37,124
  • 11
  • 56
  • 82