One Star

[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
9 REPLIES
Seven Stars

Re: [HELP] convert string to short with empty string value

short.parseShort(numID.equals("")?"0":numID)
One Star

Re: [HELP] convert string to short with empty string value

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!
Seven Stars

Re: [HELP] convert string to short with empty string value

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)
One Star

Re: [HELP] convert string to short with empty string value

i have tried in this way but gives error Smiley Sad
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)
One Star Lie
One Star

Re: [HELP] convert string to short with empty string value

(numID == null || numID.iEmpty())?null:short.parseShort(numID)
One Star

Re: [HELP] convert string to short with empty string value

where i put this code? in the t_map?
One Star

Re: [HELP] convert string to short with empty string value

i have tried but gives an error yet Smiley Sad
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)
Seven Stars

Re: [HELP] convert string to short with empty string value

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.
One Star

Re: [HELP] convert string to short with empty string value

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