Results 1 to 4 of 4
  1. #1
    codingisfun22 is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default String generation

    Hi,

    I need to create strings based upon the following input data:

    S1 = {a,b}
    S2 = {1,2,3}

    Where you take one element from S1 and combine it with S2. In this example, you would get these strings:

    a1,a2,a3,b1,b2,b3

    The number of input sets is not necessarily known, and the number of elements in each set is known before the program runs.

    Does anyone have any ideas?

    Thanks

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I'll give a simple code here. Here the output developed by using characters. So have to think about a way to concatenate those characters and make as strings.

    Java Code:
    public class StringTest
    {
    public static void main(String[] arguments)
    {
    String s1 = "abc";
    String s2 = "123";
    int s1Len = s1.length();
    int s2Len = s2.length();
    
    for(int temp1 =0;temp1 < s1Len;temp1++)
    {
    	for(int temp2 = 0;temp2 < s2Len;temp2++)
    	{
    	char ch1=s1.charAt(temp1);
    	System.out.print(ch1);
    	char ch2=s2.charAt(temp2);
    	System.out.println(ch2);
    	}
    }
    }
    }
    Check there how final strings are obtained using print() and println()

  3. #3
    codingisfun22 is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default

    Hi Eranga,

    Thank you for the response. I am trying to avoid using a for loop for each set, because I may end up with 36+ sets of characters that I may need to combine together.

    I am looking for a solution that will allow this flexibility without using multiple for loops.



    Quote Originally Posted by Eranga View Post
    I'll give a simple code here. Here the output developed by using characters. So have to think about a way to concatenate those characters and make as strings.

    Java Code:
    public class StringTest
    {
    public static void main(String[] arguments)
    {
    String s1 = "abc";
    String s2 = "123";
    int s1Len = s1.length();
    int s2Len = s2.length();
    
    for(int temp1 =0;temp1 < s1Len;temp1++)
    {
    	for(int temp2 = 0;temp2 < s2Len;temp2++)
    	{
    	char ch1=s1.charAt(temp1);
    	System.out.print(ch1);
    	char ch2=s2.charAt(temp2);
    	System.out.println(ch2);
    	}
    }
    }
    }
    Check there how final strings are obtained using print() and println()

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. EJB generation with xdoclet tags
    By gugas in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 02-18-2008, 01:07 PM
  2. random generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 03:43 AM
  3. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •