jeudi 14 novembre 2019

how to return a word from a string

I apologize for the messy code, but these are the two methods we are supposed to use. I have to find the word end in a given string, if there is no end in the string return a empty string

  • find first appearance of the word end.
  • return words up to the word end
  • ex ({"The , end, said, the ,author}tring[] output = "The, end"}tring[] */

    public static String[] endArray(String[] words) { int end = 0; for (int i = 0; i < words.length; i ++) { end ++; if (words[i] == "end") { end++; } else { end = -1; } } }

    public static int end(String[] words) { String end = "-1"; for (int i = 0; i < words.length; i ++) { end+= words[i]; if (words[i] == "end") { System.out.println(words[i-1]); end++; }

    }
    
    return end;
    

    } }

Aucun commentaire:

Enregistrer un commentaire