• Post Reply Bookmark Topic Watch Topic
  • New Topic

4 string variable concatenation  RSS feed

 
Adrian Martinez
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I am trying to concatenate 4 strings together to later be able to make them all upper case, or lower case or get the length. I keep getting an error on line 16.

anyone able to help me out?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian,
The issue is in these three lines:



Java is smart enough to realize that msg1, msg2, msg3 and msg4 have not been assigned values. So when you try to concatenate them, Java calls foul. It doesn't make sense to concatenate non-values.

 
Adrian Martinez
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i assign them a value when the input will come from the user?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are assigning them values in your code, just later. So move the concatenation statement to after that happens. Remember that the program will do things when you tell it to. Literally.
 
Adrian Martinez
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
omg that worked just by moving that section lower.

thank you,

next question though, how do you i make it give spaces between the variables? they continue typingoutlikethis
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example. You add an empty string to the list of things you are concatenating:
msg1 + " " + msg2
 
Adrian Martinez
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that worked great,

thank you
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, " " is not an empty string; it's a string with one character that happens to be a space. An empty string has no characters and is represented by "" (nothing between the two quotes).
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Technically, " " is not an empty string; it's a string with one character that happens to be a space. An empty string has no characters and is represented by "" (nothing between the two quotes).

Agreed. Thank you for correcting that.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the thread title because “4 strong variable concatenation” looked very interesting, but was probably not what you wanted to say!

Have you ever tried the String#format method?Since Strings are immutable, text1 text2 etc remain unchanged.
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:

OMG, is your other self suicidal?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, but I have to keep a low profile sometimes
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!