Say I have:
p.size = packetLine[0]; // where packetLine is of type String[] and element at that position is number represented by String
I don’t want always to write
Integer.parseInt
or reverse
String.valueOf
Eclipse gives propositions to correct the error, can I make it to advice to convert the values?
At the moment it suggests to change the type. I would like third proposition ‘Convert to Int’ or ‘Convert to String;
This is particularly annoying when repeating thousand times, I might just introduce my own method for converting like toInt or toString2, but in build solution would be better.
What you are asking for is not casting. Java uses the term casting for two operations:
Converting a numeric primitive to another numeric primitive by approximating the original value as much as possible e.g.
inttodouble.Storing the contents of an object reference variable to a more specific reference variable, if and only if the referred object can actually be stored there.
What you are asking for is conversion from String to a primitive type and vice versa. It does not usually make sense to provide shortcuts for this. There are more than one ways to do it and none is universal. E.g. a numeric String can be interpreted as an octal or a hexadecimal number and a
floatcan be converted to a String with a varying number of floating point digits, depending on the required precision…EDIT:
You might be able to make your life easier with repeated operations by creating custom editor templates in Eclipse. Editor templates are accessible along with the rest of the content assist proposals when you press
Control+Space. Template creation is not always straightforward, but it can be quite helpful in some cases.