Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

How do you solve the expression " 2 + 2 " + 3 + 4 ?

+10 votes
asked Aug 27, 2016 in CSC 211 (F16) by Alyssa Nestor (100 points)

5 Answers

+3 votes

The key issue here is that a String of text is not a number -- even if it looks like a number, it's really just text.

For strings, the + operator does concatenation, basically meaning that it puts the text of both strings together.

"cat" + "fish"   -> "catfish"

If you concatenate a string with a number, then it converts the number into text.

"moose" + 5   ->  "moose5"

So what does "2" + 5 give you?  "25"  (the String of text "25", not the number 25).

Can you apply the same logic to your larger expression of " 2 + 2 " + 3 + 4 ?

(Tip: Java works from left to right when choosing which operator to apply next -- unless there are parentheses to change the order of operations, that is.)

answered Aug 28, 2016 by Forrest Stonedahl (3,308 points)
+2 votes
String operations are confusing to me, so what helps me is remembering that everything inside of the quotation marks are left alone (even if it looks like the integers are supposed to be added together). Like Dr. Stonedahl mentioned, the usage (or lack of) of parentheses are very important here.
answered Aug 29, 2016 by Jordan Peklo (100 points)
+1 vote
You would leave the numbers and + within the quotations (don't forget that the spacing matters) and then because the numbers 3 and 4 are being added on behind the string, you would add them onto the end from left to right. So, you would add on the 3 and then the 4.  In the end, all of it will end up within the quotations.
answered Sep 1, 2016 by Bailey Aasen (100 points)
+1 vote
Any expression that includes a string will result in a string, and Java reads from left to right, following the precedence of the operators.

If you have 3 + 5 + "6" + 2 * 4 + 1, then Java will do the multiplication first, giving you

3 + 5 + "6" + 8 + 1  and then, since all remaining operators are of addition, the expression will be evaluated from left to right, and the next step will be to combine 3 and 5 as integers, giving you

8 + "6" + 8 + 1  From here, the integer 8 will be combined with the string literal "6" resulting in

"86" + 8 + 1  where again a string will combine with an integer

"868" + 1 and the same sort of concatenation occurs for an output of

"8681"   except, if this were inside a println command, it may come out without the quotation marks.

Note: Parentheses have precedence. If there are integers with a plus between them, even if they are to the right of a string, the value of the sum will be added instead of the individual numbers if they are surrounded by parenthesis.
answered Sep 1, 2016 by Sara Estrada (100 points)
–2 votes
"2 + 2 34" is the answer.
answered Aug 28, 2016 by Minh Ta (1,045 points)
Your goal should not be to post the answer.  INSTEAD, you should seek to explain the concepts involved so that the original poster (and others in the class) understand what is going on, and how to solve the problem (or another one like it) themselves.