Reputation:
<Student>
<number>122</number>
<CVF>PG</CVF>
</Student>
class Student
{
char grade
}
Now , i need to set the grade value for the Student Object above by parsing the XML Above
This CVF Element in XML can have two values either PG or MQ
As per the business , If it is PG then its value should be 1 or else 0
I defined in this way .
Student.grade = (attribute.getValue().toString()=="PG"?'1' : '0');
Please let me know if any better code will be suitable for the above requirement ??
Upvotes: 0
Views: 115
Reputation: 16409
A few points:
attribute.getValue()
is correct.attribute.getValue()
returns a string, there is no reason to call toString()
.equals()
not == when comparing Strings
, unless you know they are interned.Student.grade
unless it's a static field. You have a variable of type
Student
with a name, say s
, and then you can say s.grade
.Student s = ...
String val = attribute.getValue().toString();
if ("PG".equals(val)) {
s.grade = '1';
}
else if ("MQ".equals(val)) {
s.grade = '0';
}
else {
throw new RuntimeException("illegal value for CVF: " + val);
}
Upvotes: 1
Reputation: 11487
Dont compare Strings with "=". Its always better to do this instead.
Student.grade = ("PG".equals(attribute.getValue().toString()))?'1' : '0';
Upvotes: 0
Reputation: 66637
Assuming grade defined as char.
Student.grade = ("PG".equals(attribute.getValue().toString())?'1' : '0');
Upvotes: 0