Simple parsing techniques will also be introduced by examining 2 examples; one that handles simple parsing of an ini file and another that sums a comma delimited list of numbers. The VB and Java source is included.
LTrim is a VB Function that removes leading spaces, RTrim handles trailing spaces and Trim handles both.
Typical usage is:
MyString = " <-Trim-> " ' Initialize string. TrimString = Trim(MyString) ' TrimString = "<-Trim->".
Java provides the exact equivalent of Trim functionality - and it is called trim.
One question is, "How do I know that trim is available in Java?"
The answer to that question provides an approach to solving many programming problems that are faced in Java.
The approach starts with an educated guess and a reference to Java Classes. The guess is that Java should have sophisticated String manipulation functionality and that there might be a String class.
A good reference book is helpful, but this can all be done on the Sun website. The specific URL for the String Class and trim method is:
http://java.sun.com/products/jdk/1.2/docs/api/java/lang/String.html
The place to start is:
http://java.sun.com/products/jdk/1.2/docs/api/
That lists all classes and the class hierarchy.
Examining the class documentation on the String method, we see:
trim
public String trim()
Removes white space from both ends of this string.
Properly interpreting what this means is relatively straightforward, but can be confusing without some introduction.
public indicates that this is a public method and can be called directly.
String indicates that this method returns a String.
That is important in terms if usage.
Wrong: String test = ""; test.trim();
This is a perfectly valid statement, it just will not do anything.
To trim the String, the proper usage is:
Correct: String test = ""; test = test.trim();
The next step in our goal of creating LTrim and RTrim functions in Java is to handle the basic logic. After the logic is determined there are several options for how to bundle that logic into reuseable modules. In doing that important object oriented concepts will be introduced.
Logic
Ltrim trims the leading spaces from a String. The logic is, if the first space in the string is a character, remove it. Continue until there are no more spaces.
The code to do this is direct. Again, by examining the String Class, useful methods can be used.
String s = " test "
while(s.charAt(0) == ' '){
s = s.substring(1);
}
The charAt method returns the character at a particular position in a String. We check to see if the character at position 0 (the leading character) is a space. If it is, use substring to make a new String that starts after the space.
The logic for Rtrim is similar. While the last character in the String is a space, remove it.
String s = " test "
int len = s.length();
while(s.charAt(len-1) == ' '){
s = s.substring(0,len-1);
len--;
}
In the code, take the length of the string and use it to determine if the last character is a space. If it is shorten the string.
This gives us the basic functionality of LTrim and RTrim. The above code can be used directly in a Java program as LTrim and RTrim equivalents. That would be perfectlty acceptable. There are other methods to incorporate these functions into utility functions.
One approach to creating reausable methods for LTrim and RTrim is to create a static method in a class. In almost all cases in Java, in order to use a method that is within a class, that class must be instantiated. A static method is the exception.
To review instantion, we have used the String class extensively in these examples. The instantiation of the String class is the statement:
String s = " test "
s is the instance of the String class.
A static method can be used without instantiating the class. The best example of this is the Math.java class.
To find the square root of 9, use the static method sqrt in the math class.
result = Math.sqrt(9);
The first method for creating utility functions for ltrim and rtrim is to make them static methods in a new class called VBString.
All of the code for VBString.java and testVBString.java follows.
// class VBString
// creates static methods ltrim and rtrim
public final class VBString {
// Don't let anyone instantiate this class
private VBString() {}
public static String LTrim(String s) {
while(s.charAt(0) == ' '){
s = s.substring(1);
}
return(s);
}
public static String rtrim (String s){
int len = s.length();
while(s.charAt(len-1) == ' '){
s = s.substring(0,len-1);
len--;
}
return(s);
}
}
There is an ideal method that can generally be used to add additional methids to a class. Unfortunately, this method can not be used in this case. Certain Java classes such as the String class are declared as final. That means that they can not be extended. Extending the String class would be the ideal way to add ltrim and rtrim.
Why are some classes declared as final?
There are are 2 reasons. One is for compiler optimization and the other is simply to prevent extensions.
The following section provides details on how to extend a class, but if anyone relly wants ltrim and rtrim as part of the String class, the two avenues to pursue would be to beg Sun for this or modify the Java source code.
In an ideal world, the approach to making usefule and reuasbale ltrim and rtrim methods would be to decide that these should really be part of the String class and that any time a String is instantiated, these methods would be available. Basically, ltrim and rtrim would live in the same String class as trim.
That can not exactly be accomplished, but a new class can be created that extends String and includes our 2 new methods.
Extends is a term used extensively in the Java language. It is part of the object oriented nature of Java. A principle of an object oriented language is inheritance. In this case, we want a new class that is a String, but has 2 added features. The new class will inherit all of the String's characteristics. The new class literally IS A String. The new class is a String in the same way that a circle is a shape.
Inheritance is a powerful and useful concept and the lesson of this example should be considered and used in other cases in which extending a class can make it more useful for your purposes.
If this is confusing, please post your comments in the forum and I will try to help.
// Will not compile bacause String is a final class
public class VBString2 extends String{
public String ltrim() {
while(s.charAt(0) == ' '){
s = s.substring(1);
}
return(s);
}
public String rtrim (){
int len = s.length();
while(s.charAt(len-1) == ' '){
s = s.substring(0,len-1);
len--;
}
return(s);
}
}
Parsing
The VB and Java code for simple parsing of an ini file and a comma delimited file is included.
The interface is very simple. Enter the file name of an ini file and click on the "process ini" button. The data will be parsed and displayed.
An ini file with the line: color = red
will be displayed in the TextBox as: The color is red.
To test the comma delimited parsing, enter the file name of a file that contains a comma delimited list of numbers. Click on the "Process Comma Delimited" button and the parsed data and sum of all the numbers will be displayed.
View or download the code and ask questions about it in the Forum
parsetest.frm The VB Form
parsetest.java The Java code
comma.txt Comma Delimited file
initest.ini test ini file