A Sample Program Using Strings and Arrays
A new program using the concepts learned
We are now ready to work on a new program using the concepts we have just learned. Open the 'MySample' project in Netbeans 5.5 and use File -> New file to get to the 'new file' wizard. During the first step 'Choose File Type', select 'Java Classes' in the middle window and 'Java Main Class' in the right window and click the 'Next' button. Change the default filename to 'StringsArrays' and click 'Finish'. Delete the following constructor:
/** Creates a new instance of StringsArrays */
public StringsArrays() {
}
Now, add the following code in the 'main' method (i.e. between the curly braces right under the main method; the code should replace " // TODO code application logic here"). Make sure that each statement that ends with a ';' is on a single line (no line breaks).
double[] myDoubles =
{12.52256,-22.55989,-45.759999,100.55245,
22.359755,1000.999999,12.252122};
Double doubleHolder;
String[] myStrings;
int dotPlaceholder;
myStrings = new String[myDoubles.length];
for (int i=0;i<myDoubles.length;i++)
{
myDoubles[i] = Math.abs(myDoubles[i]);
System.out.format("Double Array Element %d: %.2f%n", i, myDoubles[i]);
doubleHolder = new java.lang.Double(myDoubles[i]);
myStrings[i] = doubleHolder.toString();
dotPlaceholder = myStrings[i].indexOf('.');
myStrings[i] = myStrings[i].substring(0,dotPlaceholder + 3);
System.out.println("String Array Element " + i + ": " + myStrings[i]);
}
Save the file. Use Build -> Compile 'StringsArrays.java' (or hit the F9 key) to compile the file and use Run -> Run File -> Run 'StringsArrays.java' to run the file. You should see the following output in the bottom window:
init: deps-jar: compile-single: run-single: Double Array Element 0: 12.52 String Array Element 0: 12.52 Double Array Element 1: 22.56 String Array Element 1: 22.55 Double Array Element 2: 45.76 String Array Element 2: 45.75 Double Array Element 3: 100.55 String Array Element 3: 100.55 Double Array Element 4: 22.36 String Array Element 4: 22.35 Double Array Element 5: 1001.00 String Array Element 5: 1000.99 Double Array Element 6: 12.25 String Array Element 6: 12.25 BUILD SUCCESSFUL (total time: 0 seconds)
- First, we declare and set an array of the primitive type double (a number with decimal digits) 'myDoubles' and initialize its seven elements.
double[] myDoubles =
{12.52256,-22.55989,-45.759999,100.55245,
22.359755,1000.999999,12.252122};
- Next, we declare a variable 'doubleHolder' that holds an Object of type java.lang.Double (not a primitive double - note that the initial 'D' is in uppercase), a string array called 'myStrings', and an integer 'dotPlaceholder'.
Double doubleHolder; String[] myStrings; int dotPlaceholder;
- Now, we allocate space for the string array. The compiler replaces 'myDoubles.length' with 7 and allocates space for seven elements. We have made the length of the string array dependent on that of the array of doubles.
myStrings = new String[myDoubles.length];
- The 'for' loop executes until the counter i becomes 6 (when i = 7, the condition i < myDoubles.Length is not satisfied as myDoubles.Length is 7)
for (int i=0;i<myDoubles.length;i++)
{
.
.
.
}
The static or class method 'abs' in the Math package is used to convert the element at myDoubles[i] to a positive number.
myDoubles[i] = Math.abs(myDoubles[i]);
- The System.out.format method substitutes the appropriate variable in the comma separated list, or a special character for each place %alphabet is encountered in the string within quotes. %f substitutes a decimal number; %.2f prints out two decimal places. %d substitutes an integer and %n inserts a new line. We use this statement to print the array of doubles
System.out.format("Double Array Element %d: %.2f%n", i, myDoubles[i]);
- We want to set the elements in the string array equal to those in the array of doubles but we cannot do this directly or even by using 'arraycopy' method as they are of different types (double and String). This is why we have to use the doubleHolder Double object. java.lang.Double contains a 'toString()' method that returns the string representation of a double. So, we use the new keyword to create an instance of a Double with the name doubleHolder to serve as a container for each element in the array. The 'java.lang' prefix is unnecessary (and not recommended); it is just used to show you where the constructor of the Double object comes from and how it works.
doubleHolder = new java.lang.Double(myDoubles[i]);
- Next, we set the element of the string array referenced by i to the value in doubleHolder converted to a string (effectively, the corresponding element in the double array)
myStrings[i] = doubleHolder.toString();
- Now, we invoke a couple of methods from java.lang.String to make changes to our output. First, we use the indexOf function to find the position of the decimal point '.'.
dotPlaceholder = myStrings[i].indexOf('.');
- The substring function returns a substring that starts at the first character of the string and ends after two decimal digits. This is appended to a descriptive label and printed out using System.out.println.
myStrings[i] = myStrings[i].substring(0,dotPlaceholder + 3);
System.out.println("String Array Element " + i + ": " + myStrings[i]);
- Note that the values of the double and the string differ in some places. This is because System.out.format rounds the double to two digits. Our invocation of the substring just truncates the string to two digits after the decimal point.