Home > Software engineering >  How to print part of a String from an array (Java)?
How to print part of a String from an array (Java)?

Time:11-11

Hello im a total beginner in Java and have a problem. In a code below that has an array of fixed list of guests, how can i print emails of these person? The email must consist of 3 first name digits and two first surname digits, and after these are @guest.com. So it looks like this:

[email protected]

[email protected]

In this task i must use methods: substring, split, toLowerCase. Sorry for my english its not perfect. Please help i've tried to solve this but i'm stuck cant manage it.

public class email {

    public static void main(String[] args) {

        String[] guests =  {      "Rock Adam", 
                                  "Stewart Thomas", 
                                  "Anderson Michael",
                                                   };
    }
}

CodePudding user response:

When you are stuck like this, try breaking down the problem bit by bit.

You are saying you don't know how to extract part of string, but also how to print. I'm tempted to give you written instructions and not the full answer to your question because that's how you will learn better.

  1. You need to construct this email for each String in the String[] array. Look for iterating over arrays in java here for example https://www.geeksforgeeks.org/iterating-arrays-java/
  2. For each String which is of this form "Rock Adam" you need to extract the surname and last name. To do this you need to split the String by space " ". How to do that - How to split a String by space
  3. When you split by space you will get another Array of two elements, first will be surname, second will be first name. Use array indecies to access them.
  4. When you access the firstName your next problem is - how do I get the first 3 characters of that String. How to access 3rd or 2nd is the same problem see how to do this here Extract first two characters of a String in Java
  5. Now that you have the substrings you want to know how to concatenate and print them. How to print multiple variables? Answer is here How to print multiple variable lines in Java. Also for transforming the strings to lowercase you can find answer here https://www.w3schools.com/java/ref_string_tolowercase.asp

Try to do some more work yourself following this and you will learn much more than from copy-pasting what someone will give you directly for free.

CodePudding user response:

Lower code solves your problem. String.split(" ") splits the String at the first occurrence of blank space. It gives a String array back which contains both parts of the name. With String.substring() you can get specific parts of the String.

public static void main(String[] args) {
    String[] guests =  {"Rock Adam",
            "Stewart Thomas",
            "Anderson Michael"};
    for(String guest : guests){
        String[] name = guest.split(" ");
        String email = name[1].substring(0,3).toLowerCase()   name[0].substring(0,2).toLowerCase()   "@guest.com";
        System.out.println(email);
    }
}

CodePudding user response:

Below code is exactly what you are looking for (i guess)

        String[] guests =  {      "Rock Adam", 
                                  "Stewart Thomas", 
                                  "Anderson Michael",
                                                   };

        List<String> emailIdList = new ArrayList<>();
        for (String guest : guests) {
            String firstName = guest.split(" ")[1];
            String lastName = guest.split(" ")[0];

            String emailId = firstName.substring(0,2)   lastName.substring(0,1)   "@guest.com";
            emailIdList.add(emailId);
        }
  • Related