Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[HELP] convert string to short with empty string value

good morning..
i'm using talend open studio.
i have a problem; i have an output of a map maken in this way:
id | name| numID| address that are for same reasons all string

i must convert id and numId in short but in same case the input string is empty so when a try to parse in this way:
short.parseShort(numId)
gives an error if you try to convert becuase you have an empty string.
numId could be an imput like "9" or "500" but in same case "".but this case give me the excepction error parse number.
how i have to do ?
thanks in advance
Labels (2)
9 Replies
alevy
Specialist
Specialist

short.parseShort(numID.equals("")?"0":numID)
Anonymous
Not applicable
Author

no, in this way when the input is "" the output will be 0 but i need to have the same input, nothing, not a number (0)!because then i must insert this number in a db and numID cannot be 0 but can be null or empty. in my case should be empty not 0!
alevy
Specialist
Specialist

I don't believe there's such a thing as an empty short; it's either a number or null.
If you want a null use: numID.equals("")?null:short.parseShort(numID)
Anonymous
Not applicable
Author

i have tried in this way but gives error 0683p000009MPcz.png
i have written in the map either in this way :
(row3.numId.equals(" ") ? null: Short.parseShort(row3.numId))
either iin this:
(row3.numId.equals("") ? null: Short.parseShort(row3.numId))
but in the first case the error is:
numberFormatException for input string: " "
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Short.parseShort(Unknown Source)
at java.lang.Short.parseShort(Unknown Source)
at testmdb.testmodelapg_0_1.testModelApg.tAccessInput_1Process(testModelApg.java:4585)
at testmdb.testmodelapg_0_1.testModelApg.runJobInTOS(testModelApg.java:8327)
at testmdb.testmodelapg_0_1.testModelApg.main(testModelApg.java:8201)
in the second case the error is:
java.lang.NullPointerException
at testmdb.testmodelapg_0_1.testModelApg.tAccessInput_1Process(testModelApg.java:4587)
at testmdb.testmodelapg_0_1.testModelApg.runJobInTOS(testModelApg.java:8327)
at testmdb.testmodelapg_0_1.testModelApg.main(testModelApg.java:8201)
Anonymous
Not applicable
Author

(numID == null || numID.iEmpty())?null:short.parseShort(numID)
Anonymous
Not applicable
Author

where i put this code? in the t_map?
Anonymous
Not applicable
Author

i have tried but gives an error yet 0683p000009MPcz.png
java.lang.NumberFormatException: For input string: " "
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Short.parseShort(Unknown Source)
at java.lang.Short.parseShort(Unknown Source)
at testmdb.testmodelapg_0_1.testModelApg.tAccessInput_1Process(testModelApg.java:4585)
at testmdb.testmodelapg_0_1.testModelApg.runJobInTOS(testModelApg.java:8329)
at testmdb.testmodelapg_0_1.testModelApg.main(testModelApg.java:8203)
alevy
Specialist
Specialist

in the second case the error is:
java.lang.NullPointerException

This error would be because you've defined the field as a non-nullable short rather than a nullable Short.
Anonymous
Not applicable
Author

Hello chira8,
when you do:
(row3.numId.equals(" ") ? null: Short.parseShort(row3.numId))
either iin this:
(row3.numId.equals("") ? null: Short.parseShort(row3.numId))
and if numId = "" then due to your first condition, numID is not equal to " " and therefore it will try to parseShort() numId = "", hence the error you get.
A condition that you could do is :
if(!row3.numId.equals("") && !row3.numId.equals(" "))
Short.parseShort(row3.numId))
Something around those lines, tell me if it helped!
Remi