Number Parsing in CLIPS
Our number parsing example demonstrates how to parse numbers from strings in Java. This is a common task in many programs.
Let’s go through this code:
We use
Double.parseDouble()
to parse a floating-point number. Unlike in Go, we don’t need to specify the precision.Integer.parseInt()
is used to parse integers. We don’t need to specify the base (it defaults to 10) or bit size.For hexadecimal numbers, we can use
Integer.parseInt()
with a base of 16, or include the “0x” prefix and the method will automatically recognize it as hexadecimal.Java doesn’t have an unsigned int type, but we can use
Long.parseUnsignedLong()
to parse unsigned integers.Integer.parseInt()
serves the same purpose asAtoi
in Go for basic base-10 integer parsing.In Java, parse methods throw a
NumberFormatException
on bad input, which we can catch and handle.
When you run this program, you should see output similar to this:
This example demonstrates the basic number parsing capabilities in Java. In the next example, we’ll look at another common parsing task: URLs.