How to convert Attributes to String? - java

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);

Related

Java & MongoDB - How to get the value of _id in a MongoDB document?

I'm using the API specified here: http://api.mongodb.com/java/current/org/bson/Document.html
Here's the code I have:
Document doc = collection.find(Filters.eq("field","value")).first();
String id = (String) doc.getString("_id"); // this line throws exception
I already checked that doc has a returned Document but I cannot access the value of _id.
The error says this:
java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to java.lang.String
_id is an ObjectId, you should use this:
String id = doc.getObjectId("_id").toHexString();
Did you try the following:
doc.get("_id");
I had to run:
doc.get("_id").toString();
To get the literal ID.

equals cannot convert from String to boolean

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':

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));

Java: JSONParseException

All I'm trying to do is to parse very simple json line, even its valid i dont know why its throwing an error
the line is
com.mongodb.util.JSONParseException:
{publish_status:'active',activation_date:{$lt:new Date()},expiration_date:{$gt:new Date()}}
^
what is wrong with the new Date() as a value?
That's not valid JSON at all. JSON syntax is defined on json.org, and it's always a string key with a value that's one of a string, number, boolean, null, array, or object. You're writing a Mongo query from Java. You should reformulate your question and retag appropriately.
I tried using the new date() in mongo DB 2.2.3 directly and it worked .. it created a value of ISODate.
You may try using this:
{publish_status:'active',activation_date:new Date(),expiration_date:new Date()}

Categories

Resources