Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Lightbulb Concatinating contents of a string array based on condition

    Hi all i have 2 string arrays

    String[] q1={"T","T,"F","T","T","F","F"}
    String[] q2={"hi","my,"name","is,"%%%","&&&","888"}
    Note q1 and q2 will alwys have the same number of values.
    and i expect the output to be as follows:

    hi
    my name
    is
    %%% &&& 888

    Logic i need to implement,
    Create target/output by scanning conditions in q1 and pulling values from q2.
    for example: take i=0
    1.look @ q1[i] it is a T now if q1[i+1] is followedd by a T pick q2[i] and place in target.
    2. if q1[i] is T and is followed by a F that means that what ever follows that particular true until the next true shd b concatinated.

    look at the above example .

    Please help me out in this , i have already done some coding, but don't get the desired output for all cases :confused:.

    Thank u


    Java Code:
    public class ConcatExample {
    	public static void main(String argv[]) {
    String[] q1={"T","T","T","T","T","T","T","F","F","F","T","T","F","T","T","T","T","T","T","T","F","T","T","T","F","T","F","F","T","T","T","T"};
    String[] q2={"PORT","X","FRTPPD","X","O/CHERB","X","ALLPALLETS","BARK","ALSO","STDS","X","PLEASE","HIGHSECURITY","X","SENDPDI","X","SHIPPERMON","X","SOLD","AGRICHEM","THOMPSON","X","ASOFMAY","REQRDDOC","COPIES","BOL","BEFORE","VIVIANA","X","SENTVIASAP","REQRDETA","X"};
    int next;
    String comment="";
    for(int i=0;i<q1.length;)
    {
    next=i+1;
      if(next<q1.length)
      {
    	  if(q1[i].equals("T")&&q1[next].equals("T"))
    	  {  System.out.println(q2[i]);
    	  System.out.println("CC");
    	   i=i+1;
    	  }
    	 else if(q1[i].equals("T")&&q1[next].equals("F"))
    	  {
    	  comment=q2[i];  
    		  while(next<q1.length) 
    		  {
    		   if(q1[next].equals("F"))
    		   {comment=comment.concat(q2[next]);
    			next++;
    			}
    		  else if(q1[next].equals("T"))
    		   {break;}
    		  }//end of while
    		System.out.println(comment);
    		System.out.println("CC");
    		i=next;
    
    	   }  //end of elseif
      } //end of if
     	else if(next>q1.length&&q1[i].equals("T"))
    	{
     	System.out.println(q2[i]);
     	System.out.println("CC");
    	} //end of elseif
    } //end of for loop
    
    	}//end of main
    }
    Last edited by gangsterooseven; 10-07-2009 at 03:15 PM. Reason: to better read the code

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Could you try and edit your post so that it can be read? Put [code] at the start of the code and [/code] at the end.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Also all that .equals("T") business is a bit tedious. The first thing I'd do is create an array of boolean and use that instead of q1.

Similar Threads

  1. IF-Condition in a String
    By lenaz in forum Advanced Java
    Replies: 1
    Last Post: 07-18-2009, 01:07 PM
  2. Concatinating a String
    By gwithey in forum New To Java
    Replies: 4
    Last Post: 03-30-2009, 02:34 PM
  3. Printing the contents of an array of objects
    By Mr.Paplu in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 05:49 PM
  4. Creating objects based on a String value
    By lvh in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 03:00 PM
  5. problem with using string in if condition
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 11:40 PM

Posting Permissions

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