Parse Method in Java
- The Scanner class provides a range of automated parsing methods any program can use. By declaring and instantiating an object of the Scanner class, passing the details of the input resource, a program can process input data as tokens. The methods of the Scanner class allow programmers to create iterative structures in which an external file is processed in chunks. The programmer can choose methods for specific types of token, including text string lines, words and number types. The following example code demonstrates reading numbers from a file using a scanner instance:
scanner.nextInt(); - The Java language includes a group of wrapper classes for primitive type values. These classes provide programmers with the ability to parse text strings as number types such as floats, doubles and integers. The following sample code demonstrates calling a method on the integer class to parse a text string:
int number = Integer.parseInt(myNumberString);
The method takes a string parameter and returns a value of primitive type integer. If the passed string parameter does not contain a value that Java can parse as an integer, the program may throw a Number Format Exception when the parse method executes. - Java applications often provide interfaces with data. This data may be stored within a database, but for many Web applications, the data is modeled in XML files. Java provides a set of code libraries for processing XML data, particularly the SAX (Simple API for XML) resource. Using the SAX library, programmers can implement separate methods for when the parser encounters the start and end tags of elements. Inside these methods, code can process the element content as in the following example:
String name = attributes.getValue("type");
This code would work with the following element opening tag, which has an attribute value:
<client type="corporate"> - Java programs can use regular expressions to match patterns in parse data. The Pattern class provides programs with the ability to define patterns of character to match in incoming text. For example, the following text could represent input data for a Java program:
name=mary&age=52
To match the values to data variables, the program can define regular expressions reflecting these structures. The Pattern class can use regular expressions to define varying sequences of character, including letters, numbers and punctuation symbols.
Scanning
Numbers and Strings
XML
Regular Expressions
Source...