WeiJie Lim wrote:
For the above code, we have to use charAt(0) to get the character that is entered from sc.next() right ? Is it because the Scanner class do not have a method to grab char ?
You can answer that for yourself. Look at the docs for Scanner. Do you see a method there that returns the first character of the input stream?
For String details = ""; , what is the purpose of "" ? I removed them and there is a slew of errors..
Local variables do not have a value until you explicitly set one. So if you just do
String details;, the
details variable does not have any value.
Later, when we do
details += ..., that says, "take the current value of 'details', and append something to the String object stored there." However, since
details doesn't have a value (because we didn't give it one), that statement cannot succeed. We can't operate on the "current value" when there is no "current value."
The
= "" part says that we are assigning to the "details" variable a reference value that points to an empty String object. That is, a String object that contains no characters. In the end, we're basically saying, "Here's an empty box. Now add this stuff to whatever is in the box."