user10713221
user10713221

Reputation:

Get a certain substring out of a String

To give you a little background of what the program does, the user enters a name of an author, and I need to check a file to know which article the person wrote. For the part that I have trouble with, I need to output only the wanted author's FULL NAME (given in the file).

How can I get only the FULL NAME of the wanted author? Thanks

Here is an example of the code I have so far (will create a method off that):

    int indexOfAnd;

    String authors = "J. Park and J. N. James and Q. Li and Y. Xu and W. Huang";
    String authorName = "James";
    String authorFullName = "";

    do
    {
        indexOfAnd = authors.indexOf("and", 0);
        authorFullName = authors.substring(indexOfAnd+4);
        System.out.print(indexOfAnd);
    }
    while(!authorFullName.contains(authorName));


    System.out.print(authorFullName);//Desired output: J. N. James

Upvotes: 0

Views: 82

Answers (4)

user1953222
user1953222

Reputation: 110

String authors = "J. Park and J. N. James and Q. Li and Y. Xu and W. Huang";
String authorName = "Huang"; 
Optional<String> name = Arrays.stream(authors.split(" and "))
     .filter(author -> author.contains(authorName))
     .findFirst();
if(name.isPresent()) {
     System.out.println(name.get());
}

Upvotes: 0

Harishma A
Harishma A

Reputation: 61

Try the below code:

    public static void main(String[] args){
            String authors = "J. Park and J. N. James and Q. Li and Y. Xu and W. Huang";
            String authorName = "James";
            String[] authorFullNames = authors.split(" and ");
            String desiredAuthorFullName = Arrays.stream(authorFullNames)
                                  .filter(authorFullName ->authorFullName.contains(authorName))
                                  .collect(Collectors.joining(" "));
            System.out.print(desiredAuthorFullName);
        }

Upvotes: 0

Zain Arshad
Zain Arshad

Reputation: 1907

You have no need to use indexOf, it will make the logic more complex. As authorNames are separated by and, this can act as a key to this problem.

  1. Separate given string into string[] by using string.split().
  2. search for you required string in that array.
  3. Print the result.

Working code:

public class stackStringSearch
{
    public static void main(String[] args)
    {
        String authors = "J. Park and J. N. James and Q. Li and Y. Xu and W. Huang";
        String authorName = "James";    
        System.out.println(searchName(authors,authorName));
    }

    public static String searchName(String authors,String authorName)
    {       
        //converting array of strings splitted by "and"
        String[] authorsArray = authors.split("and");

        for(String name: authorsArray)
        {
            if(name.indexOf("James") > -1 )  // name is found 
                return name;
        }
        return authorName;
    }
}

Upvotes: 1

KevinO
KevinO

Reputation: 4403

I would suggest you split the input String into its authors and then loop through the resultant return looking for the author you wish.

High level example:

public static void main (String[] args) throws java.lang.Exception
{
  String authors = "J. Park and J. N. James and Q. Li and Y. Xu and W. Huang";
  String authorName = "James";

  String[] parts = authors.split(" and ");

  for (String p : parts) {
    if (p.contains(authorName)) {
      System.out.println(p);
    }
  }
}

In this way, you are dealing with the tokens you want to search for, rather than trying to do offsets. It is not the only approach, however. It also assumes, similar to the original code, that:

  • An author's name doesn't actually contain the word " and "
  • That the String is not null on the input
  • That the desired author can be uniquely identified by the contains method. In other words, this approach will fail if one has "J. N. James" and "James Joyce" in the same input String. But that is perhaps a different issue.

Upvotes: 3

Related Questions