-2

I am working on a method where it returns a string and has accepts two string parameters. One of those string parameters will be a sentence and the other would be a word that I want in all caps in the other parameter's sentence.

My instructions read as follows:

Method capitalizeMatches which accepts two parameters: a haystack of type String and a needle of type String.

It returns a String in which most occurrences of the needle in the haystack are replaced with a capitalized version of the needle.

For example, capitalizeMatches("Inch by inch, life's a cinch. Yard by yard, life's hard.", "yard") → Inch by inch, life's a cinch. YARD by YARD, life's hard.

Replace the occurrences of the needle that are entirely lowercase (yard) and those whose first letter only is capitalized (Yard).

Allow the needle to appear inside other words (shipYARD).

Anyways, where is what I have so far. Down below I explain my problem/error. Help me out.

public static String capitalizeMatches(String haystack, String needle)
{
  String update = "";
  needle = needle.toUpperCase();
  String words[] = haystack.split(" ");

for(int i = 0; i < words.length(); i++)
{
  String word = words[i]; //Exception takes place somewhere here... 
  if(word.equalsIgnoreCase(needle))
  {
    word = word.toUpperCase();
  }
  update += word;
  update += " ";
}
return update;
}

If we can, I would like to stay away from arrays, but if that's the only solution to solving this, I will take it.

I'm getting an output where only it stays capital one time. I need it to always be capital.

output: Inch by inch, life's a cinch. YARD by yard, life's hard.

needs to be: Inch by inch, life's a cinch. YARD by YARD, life's hard.

Adan Vivero
  • 422
  • 12
  • 36
  • Possible duplicate of [How to capitalize the first letter of a String in Java?](https://stackoverflow.com/questions/3904579/how-to-capitalize-the-first-letter-of-a-string-in-java) – Matthew Gaiser Oct 02 '19 at 00:12
  • @MatthewGaiser that's first letter. I'm talking about particular words that is in the parameter which is a string. – Adan Vivero Oct 02 '19 at 00:15
  • Are you looking for a fix to your out of bounds exception or do you want to know how to capitalize a word? You have done the latter right already. – Dennis B. Oct 02 '19 at 00:18
  • Capitalize the word. I fixed the bounds exception problem. It's not giving me the output I'm expending however... @DennisB. – Adan Vivero Oct 02 '19 at 00:30
  • 1
    Why don't you just use a `regular expression` and `replaceAll`? – WJS Oct 02 '19 at 00:36
  • @WJS explain, because how would I replace all when I was a particular word to replace with just capital letters? – Adan Vivero Oct 02 '19 at 00:38

3 Answers3

1

In the for-loop, you need to change the condition. Not going over the char-length of haystack but over the "number of words" in words:

for(int i = 0; i < words.length; i++)
Islingre
  • 2,030
  • 6
  • 18
  • That helped with my error with out of bounds thanks. Right now it only capitalizes one of the words in the sentence. I'll update it soon and show what the output is. Thank you! – Adan Vivero Oct 02 '19 at 00:27
1

The replaceAll method uses the Pattern class to employ a regular expression search for a word and replace it with another word.

You can prefix your word to replace with a Pattern switch to ignore case when searching for the word.

String also has a toUpperCase() method which would be useful.

You should be able to do your task with a single statement. In other words all versions of yard or Yard or YaRd as part of a word or not would be replaced with YARD. If you only want whole words replaced, then check out the word boundary feature of the Pattern class.

WJS
  • 36,363
  • 4
  • 24
  • 39
  • Okay makes sense, but when I write this in my method haystack.replaceAll(needle, needle.toUpperCase()); the replaceAll() is ignored and I don't know why. (needle = yard) from the parameters. When I do haystack.replaceAll(haystack.equalsIgnoreCase(needle), needle.toUpperCase()); it says that it's a boolean and gives me an error. What's the correct way to do this then? i'm just curious, because I do feel like this method shouldn't be too long. I don't know how you want me to implement the replaceAll(). – Adan Vivero Oct 02 '19 at 00:54
  • prefix `(?i)` to your first value. That is how you set switches in the regex. The `i` stands for ignore case. Do it your first way. Don't use `equalsIgnoreCase` – WJS Oct 02 '19 at 01:04
  • haystack.replaceAll("(?i)" +needle, needle.toUpperCase()) – WJS Oct 02 '19 at 01:07
  • Thank you for this! I found my solution! – Adan Vivero Oct 02 '19 at 01:09
0

I give credit to @WJS for solving this solution for me. I kept it very basic and short. The solution is

public static String capitalizeMatches(String haystack, String needle)
{
   return  haystack.replaceAll("(?i)" +needle, needle.toUpperCase());
}
Adan Vivero
  • 422
  • 12
  • 36
  • Thanks but you should probably delete this since it's basically a comment and not answer. Some other user can benefit by reading thru the comments, etc. – WJS Oct 02 '19 at 01:29