Class 1, Part 2 | Intro to Java 30-IT-396 |
A String Demo/Lab
/**
*
* class StringManip
*
* Shows off some of the valuable String manipulation functions.
*
* @author jonesbr@email.uc.edu
* @version 1.0.0
*/
public class StringManip {
// Some variables we will use throughout this
program.
// These are private, meaning they can only
be changed by this class itself.
private int firstNumber = 1;
private int secondNumber = 2;
private char firstChar = 'a';
private String myString = "This is the string
I shall manipulate.";
/**
* StringManip(String args[])
*
* The constructor of the StringManip object.
This method runs when a new
* StringManip object is created.
*
* args[] is the String array of command
line arguments.
*
* Based on the length of args[], or the
number of Strings in args[], the
* constructor (re) sets the values of
the private variables above.
*
* Note: In general, I strongly recommend
NOT using switch-case. It just
* so happens that this is one of the rare
times when switch-case is a good
* fit.
*
*/
public StringManip(String args[]) {
// Use the length of
the args array to determine which variables to set
// from user input.
switch (args.length)
{
case 4:
firstChar = args[3].toCharArray()[0];
case 3:
secondNumber = Integer.parseInt(args[2].trim());
case 2:
firstNumber = Integer.parseInt(args[1].trim());
case 1:
myString = args[0];
}
}
/**
* evaluate runs methods on the given variables
and prints the data to
* the standard output via System.out.println().
*
* jonesbr@email.uc.edu
* January 7, 2002
*
*/
public void evaluate() {
System.out.println("The
String length is: " + myString.length());
System.out.print("The
substring generated by starting position " + firstNumber);
System.out.print(" and
ending position " + secondNumber + " is: ");
System.out.println(myString.substring(firstNumber,
secondNumber));
System.out.println("The
position of character " + firstChar + " is: " + myString.indexOf(firstChar));
System.out.println("The
character at position " + secondNumber + " is: " + myString.charAt(secondNumber));
}
/**
* public static void main(String args[])
*
* This method is called from the command
line to start the program.
* The command line arguments are inserted
into the args[] String array.
* Each String, separated by spaces, will
be a new element in the array.
*
* The first line gives the usage of the
class. In other words, it tells what
* arguments can be passed. This
is common for command line programs, though
* it is usually only printed when incorrect
parameters are entered.
*
* To properly use this class, a new object
must be instantiated. This is the
* job of the second line.
*
* Once the object is instantiated, and
only then, can the evaluate() method run.
*
* jonesbr@email.uc.edu
* January 7, 2002
*
*/
public static void main(String args[]) {
System.out.println("Usage:
[String_to_manipulate] [firstIndex] [secondIndex] [character]");
StringManip stringManip
= new StringManip(args);
stringManip.evaluate();
}
}
Created by: Brandan
Jones January 4, 2002