-2

I have the following method. If the word is found in the string, the method should return it.

    public String findWord(String string, String word){
        String[] text = string.split(" *");
        String retVal = "Not found";
        for(int i=0; i<text.length; i++){
           if(text[i].equals(word)){
               String retVal = text[i];
            }
        }
        return retVal;
    }
}

Since the retVal string can't be overriden, how do I return it?

2
  • 1
    Use retVal = text[i]; not String retVal = text[i]; Commented Dec 13, 2016 at 20:01
  • Well you're currently trying to redeclare the variable rather than just assigning a new value.
    – Jon Skeet
    Commented Dec 13, 2016 at 20:02

3 Answers 3

1

Have you try this ?

public String findWord(String string, String word){
    String[] text = string.split(" *");
    for(int i=0; i<text.length; i++){
       if(text[i].equals(word)){
           return text[i];
        }
    }
    return "Not found";
}

It's work for me.

2
  • Just a note: your code has different behavior to what's in the question, it returns the first match instead of the last one.
    – hotkey
    Commented Dec 13, 2016 at 20:07
  • 1
    Yes my code is differents but i thinks it is useless to iterate all string array because the word is found
    – Mattasse
    Commented Dec 13, 2016 at 20:09
0

Would this not work for you?

public String findWord(String string, String word){
        if (string.contains(word)) {
            return word;
        }
        return "Not found";
    }
0

Redeclaration is possible in many languages but not in JAVA. The JAVA compiler knows that retVal already exists within the same scope and is declared of type String. So it will not redeclare it, neither with same data type or different. You can either have a separate variable for this purpose or simply override the data in retVal. So, the below code must fulfil your needs.

 public String findWord(String string, String word){
    String[] text = string.split(" *");
    String retVal = "Not found";
    for(int i=0; i<text.length; i++){
       if(text[i].equals(word)){
           retVal = text[i];
        }
    }
    return retVal;
}

Now the syntax for string.split is

   public String[] split(String regex,
         int limit)

so your delimiter should be a valid expression. So you can either separate the words in your sentence using a space like this

  public String findWord(String string, String word){
String[] text = string.split("  ");
String retVal = "Not found";
for(int i=0; i<text.length; i++){
   if(text[i].equals(word)){
       retVal = text[i];
    }
}
return retVal;
 }

 void main()
 {
   System.out.println(findWord("Hello how are you","are"));
 }

But if you want to use *, you can use this code.

   public String findWord(String string, String word){
String[] text = string.split("\\*");
String retVal = "Not found";
for(int i=0; i<text.length; i++){
   if(text[i].equals(word)){
       retVal = text[i];
    }
}
return retVal;
   }
     void main()
{
  System.out.println(findWord("Hello*how*are*you","are"));
}

The output in both the cases will be- are

You can go through the following documentation about regex expressions https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#sum

2
  • This will return "Not found" even if the string contains word. :(
    – NoSector
    Commented Dec 14, 2016 at 4:21
  • @mihaijulien I edited my answer, to suit your query :)
    – Anu
    Commented Dec 14, 2016 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.