Number Parsing in Minitab
Our number parsing example demonstrates how to parse numbers from strings in Java. This is a common task in many programs.
Let’s break down the key points:
Java provides wrapper classes like
Double
,Integer
, andLong
for parsing numbers from strings.Double.parseDouble()
is used to parse floating-point numbers. Unlike in some other languages, we don’t need to specify precision.Integer.parseInt()
is used for parsing integers. By default, it assumes base 10, but you can specify a different base as a second argument.For hexadecimal numbers, you can either use
Integer.parseInt()
with base 16, or include the “0x” prefix in the string and use base 16.Java doesn’t have an unsigned integer type, but
Long.parseUnsignedLong()
can be used to parse unsigned integers up to 64 bits.There’s no direct equivalent to
Atoi
in Java, butInteger.parseInt()
serves the same purpose for parsing base-10 integers.Instead of returning an error, Java’s parse methods throw a
NumberFormatException
when given invalid input. We use a try-catch block to handle this.
To run this program:
This example demonstrates the basic number parsing capabilities in Java. Next, we’ll look at another common parsing task: URLs.