Four Stars

Replace Array of String with another

Hello, i have this code:
context.SQL=input_row.SQL;
String arrstr[] = {"Red","Blue","Green","Black","Yellow"};
String random = (arrstr[new Random().nextInt(arrstr.length)]);
String upstr = arrstr.replace(context.SQL, random);
System.out.println( upstr );

the context.SQL has some strings in it which are the cells from a sql column,i want to replace each of them  with random one from the String arrstr array but can't use the same twice, is there a way to do it please if anyone knows how? I keep getting errors.

1 ACCEPTED SOLUTION

Accepted Solutions
Fifteen Stars

Re: Replace Array of String with another

OK, I suggest you use a tJavaFlex for this. I have knocked up a quick example using the same column names as you. Here is my tJavaFlex code.

 

Start Code

// start part of your Java code
String arrstr[] = {"Red","Blue","Green","Black","Yellow"};
java.util.ArrayList<String> arrayList = new java.util.ArrayList<String>(java.util.Arrays.asList(arrstr)); 

      

I created an ArrayList from your array. 

 

Main Code

// here is the main part of the component,
// a piece of code executed in the row
// loop
String value = row1.SQL;
String random = "";

if(arrayList.size()>0){
	int pos = new java.util.Random().nextInt(arrayList.size());
	random = arrayList.remove(pos);
}
System.out.println( value + "|"+random );

The code above is fired for each row. The Start Code is fired only once at the beginning of the Subjob. In the above code, we receive the SQL value. We then check to see how many values are left in the ArrayList. If it is over 0, we retrieve a random value from what is left, remove it and use it to replace the original value (well, I don't actually replace it, I just print it out with the original value). To replace the value just use ....

value = random;

Hope this helps

 

Rilhia Solutions
4 REPLIES
Fifteen Stars

Re: Replace Array of String with another

This isn't very clear.

I assume that this code is in a tJavaRow. The context.SQL is receiving a column of data from a previous component. Does context.SQL contain a series of Strings for each row returned? For example, do the rows look something like this....

 

1) This is a series of Strings

2) This is another series of Strings in my second row

3) and a third series of Strings

4) I like dogs

5) and surprisingly I like elephants too

 

....or do the rows return records like this....

1) There

2) Is

3) One
4) Word
5) Per

6) Row

 

...?

 

If it is the first, do you want every word in the String to be replaced? If it is the second, do you just want each word replaced?

 

Second question is about using the replacement values more than once. If, looking at the above examples, the first example suits your requirement, do you want each word in the String to be replaced by different words in your array until you run out of words? If so what happens if there are 10 words in your String and only 5 in the array? If the second example suits your requirement, are you saying that the data is to be replaced with the array values until we run out of array values? So if there are 10 rows of data and only 5 array values, only the first 5 rows will be replaced, the rest will be left?

 

Can you give some examples of precisely what you want? 

Rilhia Solutions
Four Stars

Re: Replace Array of String with another

It's like in the 2nd, just 1 word per row, well i suppose if there are more words than the array could be hard to make the array words be re-utilized if all have been used at least once so just replacing with a random one is gonna be fine, been having it hard since when i give the context.SQL the value of input_row makes it a string type it pops the error that later can;t match string and string(). Thanx a lot for your help.

Fifteen Stars

Re: Replace Array of String with another

OK, I suggest you use a tJavaFlex for this. I have knocked up a quick example using the same column names as you. Here is my tJavaFlex code.

 

Start Code

// start part of your Java code
String arrstr[] = {"Red","Blue","Green","Black","Yellow"};
java.util.ArrayList<String> arrayList = new java.util.ArrayList<String>(java.util.Arrays.asList(arrstr)); 

      

I created an ArrayList from your array. 

 

Main Code

// here is the main part of the component,
// a piece of code executed in the row
// loop
String value = row1.SQL;
String random = "";

if(arrayList.size()>0){
	int pos = new java.util.Random().nextInt(arrayList.size());
	random = arrayList.remove(pos);
}
System.out.println( value + "|"+random );

The code above is fired for each row. The Start Code is fired only once at the beginning of the Subjob. In the above code, we receive the SQL value. We then check to see how many values are left in the ArrayList. If it is over 0, we retrieve a random value from what is left, remove it and use it to replace the original value (well, I don't actually replace it, I just print it out with the original value). To replace the value just use ....

value = random;

Hope this helps

 

Rilhia Solutions
Four Stars

Re: Replace Array of String with another

Works perfectly ,thanx a lot for all your help ^^