Converting Numbers to Strings

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:

int year = 2018;
String yearSentence = "the year is " + year;

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 yearString = year;
Fortunately, the String class has a method called valueOf which 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);

Using the Integer class

Alternatively, the Integer Class also has a method which will convert an int to a String as follows:

int year = 2018;
String yearString = Integer.toString(year);
Whilst this code would have the same result as above, Integers toString method 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

Task - conversion to binary

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

Type Conversion (Parsing)

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:

String numberInStringFormat = "654";
int num = Integer.parseInt(numberInStringFormat); //number will be 654
This method can also take a radix value as follows
String hexNumberInStringFormat = "A4";
int num = Integer.parseInt(hexNumberInStringFormat, 16); //value is 164

Task - sum of ints in sentence

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.

A video walkthrough of this task is available

Task - more flexible parsing

The parseInt and valueOf methods are very strict - they will throw an exception if the contents of the string are not entirely numeric.

  1. Write a method called intFromString which 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
  2. Create unit tests to thoroughly test your method.
  3. Modify your code so that it can handle Strings containing negative numbers
  4. Add further unit tests to verify the added functionality
  5. Modify the method to handle the thousands seperate (e.g. a comma ',')
  6. Add more unit tests to verify the functionality

Further task - flexible double parsing

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.