Once diners get over their initial reservations about the proper color of breakfast foods, they fall in love with green eggs and ham. Sam-I-Am has big plans to open a national chain of restaurants featuring his signature dish. He'll launch the Green Eggs and Ham restaurant chain as soon as he can find suppliers of properly colored eggs. We will write a program that replaces the text "green eggs and ham" with "Green Eggs And Ham".
Going national
- 10
Locked
Comments (31)
- Popular
- New
- Old
You must be signed in to leave a comment
RodYokoo
18 April, 17:55
Hint: Array[].split() don't stores space between words. If theres more than one space between words, the program should put it.
0
HelloWorld
22 January, 08:29
Why can't we do this instead of turning the string to a char array?
+1
Gellert Varga
20 May, 20:00
👍
0
Anonymous #10775689
1 August 2021, 11:27
Java Program to Capitalize the first character of each word in a String
This works and is an interesting method.
0
Justin Smith
15 July 2021, 18:56
This one is only hard in the sense that you have to use String methods that we haven't used or seen before. Google is your friend. Think about what you need to do with the string, and if you ask the right question Google will display the answer.
Google (or other search tool) is an essential tool for any programming language. Nobody expects a programmer to memorize every single method or function in existence. You'll have to get used to looking things up.
+2
Adam Odoj
29 November 2021, 14:31
That´s very interesting advice. I was learning in old school mode to try to memorize what I can. THANKS!!!
0
Naughtless
28 May 2021, 06:56
So if any of you were confused by the lackluster instructions on this one, here are some additional parameters that might help you:
- You need to account for if the user input has more than one space inbetween the words.
- When you print out the text, you only need one space between words, so you can eliminate the extra spaces. (You don't need to print out the extra spaces exactly like the input, which I did, and in fact won't pass)
Some tips:
- Use split().
- Then trim() the spaces.
- Learn about substring() or convert the trimmed array into individual char arrays and toUpperCase() on the first letter.
- And (credit to Steve, somewhere in the comments) split by "\\s+". Which notates splitting by one or more white spaces. If you want to look this up, its a regex expression.
+2
ImDevin
3 May 2021, 02:23
As someone previously said, another forced self-learning, but this one is pretty ridiculous in its scope (stackOverFlow suggests 20+ ways to solve it & only understand parts of each solutions). Got it to pass, but gonna have to some more time studying these methods and their logic. Fooyee!!
0
ayhem bouabid
7 January 2021, 08:57
Well, I have seen many comments about changing the string itself, but I think such a choice is not that favorable after all. For me, I decided to make a different string variable: resultSentence. then I wrote a method that returns the indices of the first letters in each word in the sentence aka the indices that come right after the space character. Using the return list, I can know which of the characters need to be uppercase and which are lowercase.
0
Karas Java Developer
23 October 2020, 00:41
Yes these are your best friends for this task:
.split()
.charAt()
.substring()
and use just System.out.print instead of println, depending on your code.
As an additional note, make sure to test your answer with a string that has 2 or more spaces. That one got me spining for some minutes.
+2
ImDevin
3 May 2021, 02:48
and .trim()
+1
ziv fisher
14 October 2020, 07:58
For this task you need to first Google:
1. Scanner scanner = new Scanner("string"); //take a sentence and divide it to single words.
2. Character.toUpperCase(i); // take a letter at index(i) and turn it into upperCaseLetter.
3. "string".subString(i);
4. StringBuilder - it helps to learn about it as well but not mandatory.
+1
Andrei
6 November 2020, 09:09
But why would you scanner when Codegym recommends using BufferedReader?
+1
Adam Odoj
29 November 2021, 14:35
I agree! I'm still using Buffered Reader but whereever I look for google examples, eveyone uses scanner, that's odd, waiting for answers soon...
0
Pavel Naumovich
30 September 2020, 17:15
.split(" ")
Character.toUpperCase
charAt
are your friends
0