Search code examples
javaemailnames

Make an email list, by entering only recipient names


I want to make a simple code, that prompts you to enter names, separated by comma or just a space, and when you click enter, to take every one word you entered, and put a @gmail.com at the end of it, how can I do it? That's what I have for now

    Scanner input = new Scanner(System.in);
    String mail = "@gmail.com";
    String names;
    System.out.println("Enter names: ");
    names = input.next();
    System.out.println(names + mail);

Solution

  • This should be everything you asked for, if you put a list of names separated by commas it will loop through them, otherwise it will just print a single name.

        Scanner input = new Scanner(System.in);
        String mail = "@gmail.com";
        System.out.println("Enter names: ");
        String names = input.next();
        if(names.contains(",")) {
                for(String name : names.split(",")) {
                        System.out.println(name + mail);
                }
        } else {
                System.out.println(names + mail);
        }
    

    Hope that helps.