0

I can prompt the user for names and have my do while continue to ask them for the names but I have no idea how to store them into an ArrayList. I have seen a few examples but they all require the user to input a set number of elements.

I just need to prompt the user to input names, then continuously ask and prompt the user to input more names. Then eventually print all the names. Until the user types N

import java.util.Scanner;

public class Main {
  
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        ArrayList<People> list = new ArrayList<People>();
        String a = ""; 
        do{
            System.out.println("Enter your first and last name: ");
            String name1 = scan.nextLine();

            System.out.println("Would you like to enter another name? (Y/N)");
            a = scan.nextLine();
        } 
        while(a.equalsIgnoreCase("Y"));

    }
}
EricSchaefer
  • 25,272
  • 21
  • 67
  • 103

1 Answers1

0

try with following solution,

People.java

public class People {

    private String name;
    // other variables of People

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    // other getters and setters
}

Main.java (Edited)

public class Main {
      
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        ArrayList<People> list = new ArrayList<People>();
        String a = "";
        boolean contain = false; // declare the boolean variable for store the status of name matching
        
    do {
        System.out.println("Enter your first and last name: ");
        String name1 = scan.nextLine();
        People people = new People(); // initialize a new people object
        people.setName(name1); // set the name to above people
        list.add(people); // add people object into ArrayList of People

        System.out.println("Would you like to enter another name? (Y/N)");
        a = scan.nextLine();
    } while(a.equalsIgnoreCase("Y"));
    
    
    System.out.println("Pleaes enter the name for search");
    String name = scan.nextLine();
    
    for (int i = 0; i < list.size(); i++) {
        if(list.get(i).getName().equals(name)){
            System.out.println(name+" is in position "+i);
            contain = true; // set contain variable to true when match the given name with people in ArrayList
            break; // if match the name, break the loop
        }
    }
    if(!contain){ // if not contain the people with given name in ArrayList, execute following message
        System.out.println("this people not contain in list");
    }

    }
}

please refer this question for more ways about how to search custom object in ArrayList

Lakshan
  • 1,404
  • 3
  • 10
  • 23
  • That helped a lot thank you, if I wanted to search in this ArrayList how would I start? Lets say, I entered names like Apples Oranges Tomatoes Cucumber, If I wanted to search for Cucumber I would want it to reply like "Cucumber is in position 4" – Brandon Zywoo Apr 04 '21 at 06:06
  • @BrandonZywoo please refer above modified answer – Lakshan Apr 04 '21 at 06:21