One Star

Need Help for if - Else in tJavaRow

Hi,
I'am a beginner on Talend and Java language.
I do a job who request on Informix DB with two tMap and one tUnit for generate a Directory of personns. The Job works fine. But i have a problem for reverse column at the end of job in my csv.
in fact : My csv contain : Name - married name - FirstName. I just want : if married name is different of null, the name = married name.
At the end of my job, juste before generate my final csv, i have add between my tUnit and the cvs output file a tJavaRow coponent, who i haved generate the java code correctely
(exemple Smiley Surprisedutput_row.nom_pat = input_row.nom_pat; (name)
output_row.nma_pat = input_row.nma_pat; (married name)
And i have add this code :
if (input_row.nma_pat != null) {
output_row.nom_pat = input_row.nma_pat;
output_row.nma_pat = "";}
else { output_row.nom_pat = input_row.nom_pat;}
With that, the job runs correctly, when a line contains a married name, i have this married name in the collumn of name, but the others lines who havent married name (male) have a null name. This is my problem. I think my If - Else is not good.
I hope someone could help me !
Thanks before, and sorry for my poor english!
11 REPLIES
One Star

Re: Need Help for if - Else in tJavaRow

No one for help me ? I just need a java code for if a filed is not null, an other field = this field. But only the field who are not null and not allez my csv. Is very important. Thanks
Employee

Re: Need Help for if - Else in tJavaRow

Hi,
Please, could you explain a little bit your need.
We have several solutions to do that.
You could use a ternary expression or also add a simple routine in Code repository.
Before explain these two ways, could you describe some sample input rows, and some expected output rows.
Then we could probably help you on that need.
Best regards;
One Star

Re: Need Help for if - Else in tJavaRow

Hi,
Thanks for your answer. I will try to speak in comprehensible english Smiley Happy :
My first Job work fine, its certainly not optimize but it's work. i must generate an patient's directory for an vocal standard from informix database. i describes you my lasts components :
After tMap, request and other i have in output two csv files. This two file have got the same schemas :
-iddossier
-nom (name)
-nma (married name)
-prenom
-sexe
-den
-dso
-service
-chambre
I merges this two file with tUnit component (same schemas) on a csv output file (same schemas). The problem is that : In my database for married women , the married name is insert into "nma" and maiden name in "nom". In my directory i want the married name and not "nom".
I just want for some lines of my csv who have got a married name, swhitch this column into "nom". I hope you understand Smiley Happy For that between tUnit and ouput csv i have add an tJavaRow, i have clik on generate code and i add to that little code of java (i don't know java).
"output_row.iddossier = input_row.iddossier;
output_row.nom_pat = input_row.nom_pat;
output_row.nma_pat = input_row.nma_pat;
if (input_row.nma_pat != null) {
output_row.nom_pat = input_row.nma_pat;
output_row.nma_pat = "";}
else { output_row.nom_pat = input_row.nom_pat;}
output_row.pre_pat = input_row.pre_pat;
output_row.sex_pat = input_row.sex_pat;
output_row.den_dossier = input_row.den_dossier;
output_row.dso_dossier = input_row.dso_dossier;
output_row.ser_dos = input_row.ser_dos;
output_row.nch_sej = input_row.nch_sej;"
That work because all the "nm"a are put in "nom" collumn, thats i want. But all others name's without married name (male or women not married) disappear.
I hope u are understand what i want to do.
One Star

Re: Need Help for if - Else in tJavaRow

Hi Neneow,
try not to check on a null value but on an empty string "".
if (input_row.nma_pat != "") {
output_row.nom_pat = input_row.nma_pat;
output_row.nma_pat = "";}
else { output_row.nom_pat = input_row.nom_pat;}

Does that do the trick?
Mike
One Star

Re: Need Help for if - Else in tJavaRow

Hi Mike,
i have try with "", and the result is the same, it's work the married name are put in "nom" collumn, but all other lines who haven't married name and just one name disappear.
Thanks
Employee

Re: Need Help for if - Else in tJavaRow

Hi,
I'm not sure to understand your need as well.
Do you have some sample input rows ? JobDesign screenshot ? some sample expected rows ?
Probably I'm wrong but I believe that you have made a complex job which don't seems so tricky.

If you want try the Mike solution; try this:
if (input_row.nma_pat != null && !input_row.nma_pat.equals("")) {
output_row.nom_pat = input_row.nma_pat;
output_row.nma_pat = "";}
else { output_row.nom_pat = input_row.nom_pat;}

Best regards
One Star

Re: Need Help for if - Else in tJavaRow

Ho Yeah, it's works very well Cantoine ! Thanks you very much Smiley Tongue with your code, if input_row nma is not null, ouput_row.nom = input_row nma and now also for line where input_row nma is null i keep my ouptu_row.nom. I think you not understand yet, but tha'is right for me Smiley Happy
Now i must understand the difference with your code and Mike's code.
Thank you very much yet )
Employee

Re: Need Help for if - Else in tJavaRow

Hi,
In java when you want test if a String is not null and not empty you have to use : myVar != null && !myVar.equals("")
myVar != null => myVar not null
!myVar.equals("") => myVar is not empty
myVar.equals("") => myVar is empty
Because Mike expression (myVar != "") works for Numeric or other data type but not for String.
Best regards
One Star

Re: Need Help for if - Else in tJavaRow

Ok, i understand, rows who havent married name was null but not empty.
Best regards !

Re: Need Help for if - Else in tJavaRow

Hi,
actually, I'm selecting value using simple Oracle query in one of oraclecomponent toracleInput_1 in talend. The possible Output of this  query is 'Y'/'N'.
 I'd like to process further on value of 'Y' and want to raise error on value of 'N'. but the problem is - I don't know how to get value of previous component in Tdie_1 component or in warning component.
I want to Display message and want my Job to stop on value of 'N'. can you pls help me with code.

Re: Need Help for if - Else in tJavaRow

Can anyone pls help me on this?