Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
sushantV
Creator
Creator

String to Boolean conversion not working

In a simple tMap mapping, I need to convert a string to boolean. The data is all 0s/1s and I am using Boolean.valueOf(variable) and all I am seeing is 'false' being returned. What am I doing wrong?

 

I've validated everything using tLogRow on both the sides of the tMap. The input clearly prints all the 0s and 1s. The output of tMap is all false. 

Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable

According to java docs Boolean.valueOf(String s):

Returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".

Since your input of "0" or "1" is not "true" All will be evaluated to false

try this instead

row1.data.equals("1") ? true : false

View solution in original post

4 Replies
Anonymous
Not applicable

According to java docs Boolean.valueOf(String s):

Returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".

Since your input of "0" or "1" is not "true" All will be evaluated to false

try this instead

row1.data.equals("1") ? true : false
sushantV
Creator
Creator
Author

Yeah I figured that. Using "1".equals(column name). Slightly shorter to type.

Anonymous
Not applicable

quick tip with the .equals() and .equalsIgnoreCase() methods: If you write them "backwards" by calling the method from your string constant, you'll avoid nullpointer exceptions when your input data is null. 

 

this will not throw a NPE when row1.data is null

"1".equals(row1.data) ? true : false
BA621858188
Creator
Creator

Hi All,

 

I wanted to convert data type string to boolean. input data is T/F.

 

getting an error: Cannot convert "F" to Boolean