0

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);
Roman C
  • 49,761
  • 33
  • 66
  • 176
Sartheris Stormhammer
  • 2,534
  • 8
  • 37
  • 81

2 Answers2

0

Not knowing what language this is, here's the pseudo-code:

names = input.next();
namesArray = names.split(" ") -- replace with your preferred delimiter
foreach name in namesArray
  print name + mail
Adrian Godong
  • 8,802
  • 8
  • 40
  • 62
  • what about the foreach part and the print one? please explain – Sartheris Stormhammer Mar 30 '13 at 16:16
  • RTFM: http://leepoint.net/notes-java/flow/loops/foreach.html For print, you can use your System.out.println(names + mail); – Adrian Godong Mar 30 '13 at 16:21
  • sorry if I will sound rude, I don't know if you understand from the noob question, but I am a beginner. So could you PLEASE write the whole code with the "for" part? – Sartheris Stormhammer Mar 30 '13 at 16:28
  • well, nothing, I don't know how I can use a separator such as space, to have it do what I want... Scanner input = new Scanner(System.in); String mail = "@gmail.com"; String names; System.out.println("Enter names: "); names = input.next(); String[] namesArray = names.split(" "); for (names : namesArray) { System.out.println(names + mail); } – Sartheris Stormhammer Mar 30 '13 at 16:33
0

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.

Drew
  • 309
  • 1
  • 9
  • Exactly what I wanted, thanks! But why it doesn't work with a space? – Sartheris Stormhammer Mar 30 '13 at 16:44
  • You'll need to add a solution from this list as spaces don't work: [How do I split a string with any whitespace chars as delimiters?](http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters) – Drew Mar 30 '13 at 17:15