Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

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 0683p000009MA5A.pngutput_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!
Labels (3)
11 Replies
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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;
Anonymous
Not applicable
Author

Hi,
Thanks for your answer. I will try to speak in comprehensible english 0683p000009MACn.png :
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 0683p000009MACn.png 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.
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

Ho Yeah, it's works very well Cantoine ! Thanks you very much 0683p000009MAB6.png 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 0683p000009MACn.png
Now i must understand the difference with your code and Mike's code.
Thank you very much yet )
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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