Difference between revisions of "Concatenate Strings"

From CompSciWiki
Jump to: navigation, search
Line 52: Line 52:
 
</pre>
 
</pre>
  
 +
<BR>
 +
 +
That's it, you have combined two strings and printed the value!
 +
 +
<BR>
 +
 +
|SolutionCode=Putting it all together...
 +
 +
<pre>
 +
public static void main()
 +
{
 +
  String returnVal;
 +
 +
  returnVal = concatenate("string1", "and string2");
 +
 +
  System.out.println(returnVal);
 +
}
 +
 +
public static String concatenate(String str1, String str2)
 +
{
 +
  return(str1 + str2);
 +
}
 +
 +
</pre>
 
}}
 
}}

Revision as of 11:50, 6 April 2010

Back to the Program-A-Day homepage

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

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

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:


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 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 put them into one string. We will then return this value:

public static String concatenate(String str1, String str2)
{
   return(str1 + str2);
}


That's it, you have combined two strings and printed the value!


Code

Solution Code

Back to the Program-A-Day homepage