equals cannot convert from String to boolean - java

I want to compare input String from Database in tMap component in Talend Open Studio. If my String is equal to "{}", I want to put there "nodata" string. Otherwise I leave the original input as it is.
My code in expression/filter in tMap:
(row1.parameter).equals("{}")?"nodata":row1.parameter
Error I'm getting:
Detail Message: Type mismatch: cannot convert from String to boolean
Do you have any suggestions?

Try to assign the result to a String variable, like
String someVariable = row1.parameter.equals("{}") ? "nodata" : row1.parameter;
because your expression returns a String… Are you assigning it to a Boolean? If yes, that will cause / be a type mismatch.

Assuming you're keeping the "Parameter" field, it sounds like your Output field that you are assigning 'Parameter' to is not a 'String' field, and is set as 'Boolean':
So the first point of call would be to check/change the output to a type of 'String':

Related

Hamcrest matchers to check JSON filed value is null or empty?

I am using hamcrest for first time, i have developed a framework to validate particular JSON field values need to satisfy some business rules, i am trying to check on of the field in JSON should not be " " or null. Please find below for the code,matcherOperator validate JSON field against a Array of values for example in ISINARRAY validate the JSON filed is present in the following list of array values [1,2,3,4] similarly NOTINARRAY validate the JSON filed should not be present given range of array values. I am trying to check whether given field is empty or null i defined array like this [null,""] and using NOTINARRAY to check JSON key value shouldn't be null or "" for null its working but the JSON value is "" its validating to true and i tried using Matchers.blankOrNullString() too but its not working and i saw two question of similar type but they are part of JUNIT testing which i tried to used but not working below are the question i referred
Hamcrest matcher with slashes is interpreted as a part of validation
Hamcrest check if value is null or empty array
public <T extends Comparable<T>> Matcher<String> mymatcher(final FieldOperator matcherOperator,
final List<String> value)
{
switch (matcherOperator) {
case ISINARRAY: {
return Matchers.in(value);
}
case NOTINARRAY: {
return Matchers.not(Matchers.in(value));
}
}
I am not sure where i am doing wrong please help me in solving the issue.

String cannot be cast to Integer session Attribute

Hello I am getting this error:
java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Integer
at this line of the code:
int est;
est=(Integer) session.getAttribute("estado");
I think that the problem is that "estado" is not filled yet. I use this session-attribubute in order to see if session has been signed in and so that it doesnt need to log in again.
You cannot type-cast here, as Integer is not compatible with String.
Use Integer#parseInt to parse it as an int
est = Integer.parseInt(session.getAttribute("estado"));
The session.getAttribute("estado"); returns the session attribute held in estado, What happened here that, the JVM has found (during runtime) the returned value's type is String so when you tried to cast it into Integer it fired a ClassCastException
What you need here is to parse the result returned by the attribute estado using Integer.parseInt(session.getAttribute("estado"));, which was suggested in the answers
NOTE: you maybe questioning that you have added an integer (not a String) to that attribute, but trace your code carefully considering that request.getParameter("attrName") returns a String, just an assumption
use Integer.parseInt
int est= Integer.parseInt(session.getAttribute("estado").toString());

Filter Realm Result by Integer

There are two methods to filter realmResult in java by "string"
RealmResults data = realm.where(RasifalDTO.class).contains(keyString,valueString);
But what i want to do is filter the Result with Respect to integer so i tried:
RealmResults data = realm.where(RasifalDTO.class).contains(keyString,vauleInt+"");
But i get :
java.lang.IllegalArgumentException: Field 'rasifalType': type mismatch. Was INTEGER, expected [STRING].
If you want to filter the result by an attribute of the Realm Object which happens to be an integer. Then equalTo(String key,int value) is the way to go (Do not get confused with using contains(key string,value string) like i was ).
RealmResult data = realm.where(RasifalDTO.class).equalTo(keyString,valueInt).findAll();
Try using this:
RealmResults data = realm.where(RasifalDTO.class).contains(keyString,String.valueOf(vauleInt));

How to convert Attributes to String?

I want to convert Attributes to String so that I can trim and get a substring of the attribute value.
Here is my code:
Attribute attrs = match.getAttributes();
NamingEnumeration e = attrs.getAll();
System.out.println(attrs.get("cn"));
System.out.print(attrs.get("uniqueMember"));
unique_members[i] = attrs.get("uniqueMember");
I am facing an error in the last line where I want to store the value of uniqueMember to the unique_members array. Error:
Type mismatch: cannot convert from Attribute to String
I have tried the following so far:
unique_members[i] = (String)attrs.get("uniqueMember");
It doesn't solve the issue and I am getting error:
Cannot cast attribute to String.
Use unique_members[i] = attrs.get("uniqueMember").toString() to convert the attribute value to a string.
For more informationens see the javadoc of BasicAttribute or javax.naming.directory.Attribute
Have a look at the Javadoc of Attributes class. You can use getValue(String) to retrieve the value of a specific attribute.
I suggest to use null-safe Objects.toString() for getting string representation--it won't throw exception if attrs.get() returns null, which may happen if attribute is not found:
unique_members[i] = Objects.toString(attrs.get("uniqueMember"));
If you came here searching for how to convert any Object with its attributes to a string, you can consider using ReflectionToStringBuilder:
ReflectionToStringBuilder.toString(anyObject);

Find index position of character within string without accessing the string variable again, in Java

This is because the string is got from a ResultSet which returns no value if the string is accessed again.
My position is like this.
ResultSet rst = pst.executeQuery(); //pst is a preparedstatement
value = Integer.parseInt(rst.getString(1).trim().substring(0,<need to get position of '.' here>)
If I give rst.getString(1) again, it gives me an exception as no data found.
I have a workaround to store the string in a temporary variable and access it.
I needed to know if there was something like 'this' object to access the current string within a function of that string.
Thanks
What speaks against storing the string in a temporary variable?
You can save rst.getString(1) to new string variable
String st=rst.getString(1);
or use somthing like this
rst.getString(1).split("\.")[0].trim()

Categories

Resources