Sub topic 10
Converting Between Strings and Values
A String holds text. Even when that text contains digits, Java does not treat it as a number. For example, "24" + 1 produces "241", not 25.
To do arithmetic, first convert the text to a number. To turn a value back into text, use String.valueOf.
Convert text to a number
A whole number has no decimal part. In Java, int is a type for whole numbers. The method Integer.parseInt reads a string and returns an int:
int count = Integer.parseInt("24");Parsing is the process of reading text as a value. The text must be a valid whole number within the range an int can hold. For example, "24" and "-5" work, but "3.5" and "24 cats" do not.
If parsing fails, Java throws an exception, a signal that something went wrong. Integer.parseInt throws a NumberFormatException for invalid input. A try and catch block lets the program respond instead of stopping.
Convert a value to text
String.valueOf returns a string that represents a value:
String text = String.valueOf(25);Here, text contains "25". You can also use String.valueOf with other types, such as double and boolean.
Worked example
This program reads a number from a string, adds one, and converts the result back to text. It also shows conversions for a decimal number and a true-or-false value.
Save it as ConvertValues.java:
class ConvertValues {
public static void main(String[] args) {
String input = " 24 ";
try {
int count = Integer.parseInt(input.trim());
int nextCount = count + 1;
String result = String.valueOf(nextCount);
System.out.println("Next count: " + result);
} catch (NumberFormatException e) {
System.out.println("Enter a whole number.");
}
double price = Double.parseDouble("3.50");
String priceText = String.valueOf(price);
System.out.println("Price: " + priceText);
boolean ready = Boolean.parseBoolean("TRUE");
System.out.println("Ready: " + ready);
}
}Compile and run it:
javac ConvertValues.java
java ConvertValuesThe output is:
Next count: 25
Price: 3.5
Ready: trueThe trim method removes spaces from the start and end of the input. The program then parses "24", adds 1, and converts 25 to a string.
Try changing input to "24 cats" and running the program again. The conversion fails, so the catch block prints Enter a whole number.
Decimal and true-or-false values
A double is a type for numbers that can have a decimal part. Double.parseDouble converts text such as "3.50" to a double. Use a dot as the decimal separator, not a comma.
Notice that converting the value back produced "3.5", not "3.50". The number does not keep the original text’s trailing zero. Also, a double cannot represent every decimal exactly, so it needs care when used for money.
A boolean is a true-or-false value. Boolean.parseBoolean returns true when the text is "true", ignoring letter case. It returns false for every other input, including "yes" and " true ".
Exercise
What does Integer.parseInt("12") + 1 produce? What does "12" + 1 produce?
Show answer
The first produces the number 13. The second produces the string "121".
Exercise
What happens when Integer.parseInt("4.5") runs?
Show answer
It throws a NumberFormatException because "4.5" is not a whole-number string.