Difference between revisions of "Concatenate Strings"
From CompSciWiki
(Changed to utilize CodeBlock template) |
|||
(8 intermediate revisions by 4 users not shown) | |||
Line 3: | Line 3: | ||
|Problem= In this problem you will pass a method two strings. The Method will combine these two strings and return it. Print the returned value. | |Problem= In this problem you will pass a method two strings. The Method will combine these two strings and return it. Print the returned value. | ||
− | + | |SideSectionTitle=Static Methods | |
|SideSection= | |SideSection= | ||
− | [[Image: | + | [[Image:Wiki_method02.jpg|center]]<BR> |
− | <BR> | + | |
− | + | ||
|Solution=The solution... | |Solution=The solution... | ||
To start this problem we will set up our code structure. We will need a main method, and we will need a concatenate method: | To start this problem we will set up our code structure. We will need a main method, and we will need a concatenate method: | ||
− | + | {{CodeBlock | |
− | + | |Code= | |
− | + | ||
public static void main() | public static void main() | ||
{ | { | ||
Line 22: | Line 19: | ||
{ | { | ||
} | } | ||
− | + | }} | |
<BR> | <BR> | ||
Line 28: | Line 25: | ||
Next we will fill in main. In main we will need to pass two strings to concatenate, and print the value returned from this method: | Next we will fill in main. In main we will need to pass two strings to concatenate, and print the value returned from this method: | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
public static void main(String [] args) | public static void main(String [] args) | ||
{ | { | ||
Line 37: | Line 35: | ||
System.out.println(returnVal); | System.out.println(returnVal); | ||
} | } | ||
− | + | }} | |
<BR> | <BR> | ||
− | Next we will need to make our concatenate method. For this method we will take the two passed strings and | + | Next we will need to make our concatenate method. For this method we will take the two passed strings and append the second string to the end of the first. We will then return this new string: |
− | + | {{CodeBlock | |
+ | |Code= | ||
public static String concatenate(String str1, String str2) | public static String concatenate(String str1, String str2) | ||
{ | { | ||
return(str1 + str2); | return(str1 + str2); | ||
} | } | ||
− | + | }} | |
<BR> | <BR> | ||
Line 56: | Line 55: | ||
<BR> | <BR> | ||
− | |SolutionCode= | + | |SolutionCode= |
− | + | ||
− | + | ||
public static void main(String [] args) | public static void main(String [] args) | ||
{ | { | ||
Line 72: | Line 69: | ||
return(str1 + str2); | return(str1 + str2); | ||
} | } | ||
− | |||
− | |||
}} | }} |
Latest revision as of 16:29, 4 December 2011
Back to the Program-A-Day homepage
ProblemIn this problem you will pass a method two strings. The Method will combine these two strings and return it. Print the returned value. |
Static Methods | |
---|---|---|
SolutionThe solution... To start this problem we will set up our code structure. We will need a main method, and we will need a concatenate method: public static void main() { } public static String concatenate(String str1, String str2) { }
Next we will fill in main. In main we will need to pass two strings to concatenate, and print the value returned from this method: public static void main(String [] args) { String returnVal; returnVal = concatenate("string1 ", "and string2"); System.out.println(returnVal); }
Next we will need to make our concatenate method. For this method we will take the two passed strings and append the second string to the end of the first. We will then return this new string: public static String concatenate(String str1, String str2) { return(str1 + str2); }
That's it, you have combined two strings and printed the value!
| ||
Code |