So far we have been able to convert numeric formats, such as integers to strings simply by appending them to an existing String for example:
Unfortunately however, unless we are concatenating a number with a String, the approach does not work - the second line in the example below would error:
int year = 2018; String yearSentence = "the year is " + year;
Fortunately, the String class has a method called
int year = 2018; String yearString = year;
valueOfwhich takes various numeric types (int, float, double, long and even char and boolean) and returns a String. Thus we can convert a integer value to a String as follows:
int year = 2018; String yearString = String.valueOf(year);
Alternatively, the Integer Class also has a method which will convert an int to a String as follows:
Whilst this code would have the same result as above,
int year = 2018; String yearString = Integer.toString(year);
toStringmethod also allows us to supply a second parameter, which determines the base (or radix) used for the conversion. For example the default conversion of 10 to a string would result in "10", however if we specify hexadecimal (e.g. base 16) as a second parameter, the value returned would be 'A'. The example below shows this in action:
int year = 2018; String yearAsHexadecimal = Integer.toString(year,16); System.out.println(yearAsHexadecimal); //prints 7E2
Write a programme that will take a user input in the form of an integer and convert it to binary. Output the value in in the following format 5 in binary format is 101, by applying the techniques explained above
It is also relatively common to need to convert data from a String into a numeric format, for example, we might get some data from the user as a String, which is actually a number that we need to do some conversion with. Fortunately the Java number classes (such as Integer) provide functions to do just that. The process of coverting from a string format to a numeric format is known as parsing.
The following code provides an example where a string value is converted to an integer:
This method can also take a radix value as follows
String numberInStringFormat = "654"; int num = Integer.parseInt(numberInStringFormat); //number will be 654
String hexNumberInStringFormat = "A4"; int num = Integer.parseInt(hexNumberInStringFormat, 16); //value is 164
Consider the following sentence: "She had 4 cats, 3 hamsters, 2 dogs and 12 pet fish".
Write code that will take a line of input from the user (which will be a sentence in a form similar to that above), and programatically determine how many animals there are in total.
valueOf methods are very strict - they will throw an exception if the contents of the string are not entirely numeric.
intFromStringwhich will take a string, and return an integer corresponding to the first integer found within the string (you can disregard consideration of the minus symbol at this stage). Bearing in mind that the integer may have multiple digits. Disregard any subsequent numbers in the string (so the string "add 24 and 3" would be processed as 24). If no integers are found return 0
Write a version of the method called
doubleFromString above which will return a double instead of an integer (nagative or otherwise). Ensure you create thorough unit tests to test it, ensuring that the negative symbol and thousands seperators are accounted for.