Tuesday, December 31, 2019

Java Syntax Converting Strings to Numbers

Typically in a graphical user interface, there will be text fields that are expecting the user to enter in a numerical value. This number value will end up in a String object which doesnt really help your program if you want to do some arithmetic. Fortunately, there are wrapper classes that provide methods for converting those String values into numbers and the String class has a method to convert them back again. Wrapper Classes The primitive data types that deal with numbers (i.e, byte, int, double, float, long, and short) all have class equivalents. These classes are known as wrapper classes as they take a primitive data type, and surround it with the functionality of a class. For example, the Double class will have a double value as its data and provide methods for manipulating that value. All of these wrapper classes have a method called valueOf. This method takes a String as an argument and returns an instance of the wrapper class. For example, lets say we have a String with the value of ten: String number 10; Having this number as a String is no use to us so we use the Integer class to convert it into an Integer object: Integer convertedNumber Integer.valueOf(number); Now the number can be used as a number and not a String: convertedNumber convertedNumber 20; You can also make the conversion go straight to a primitive data type: int convertedNumber Integer.valueOf(number).intValue(); For other primitive data types, you just slot in the correct wrapper class—Byte, Integer, Double, Float, Long Short. Note: You must make sure the String can be parsed into the appropriate data type. If it cant you will end up with a runtime error. For example, trying to covert ten into an integer: String number ten;int convertedNumber Integer.valueOf(number).intValue(); will produce a NumberFormatException because the compiler has no idea ten is supposed to be 10. More subtly the same error will occur if you forget that an int can only hold whole numbers: String number 10.5;int convertedNumber Integer.valueOf(number).intValue(); The compiler wont truncate the number it will just think that it doesnt fit into an int and that its time to throw a NumberFormatException. Converting Numbers to Strings To make a number into a String follows the same sort of pattern as the String class has a valueOf method too. It can take any of the primitive data type numbers as an argument and produce a String: int numberTwenty 20; String converted String.valueOf(numberTwenty); which puts 20 as the String value of co nverted. or you can use the toString method of any of the wrapper classes: String converted Integer.toString(numberTwenty); The toString method is common to all object types—most of the time it is just a description of the object. For wrapper classes, this description is the actual value they contain. In this direction, the conversion is a bit more robust. If the Double class was to be used instead of the Integer: String converted Double.toString(numberTwenty); the result would not cause a runtime error. The converted variable would contain the String 20.0. There is also a more subtle way to convert numbers when you are concatenating Strings. If a String was to be built like: String aboutDog My dog is numberTwenty years old.; the conversion of the int numberTwenty is automatically done.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.