CSE/IT 213 - Strings
New Mexico Tech
September 7, 2011
Strings
A step up from character arrays
Can compare strings
Can search through strings for
certain character
Can split up strings on certain
character
2
Info about strings
Java API
see Class String
Lets take a look
General form of Java Program
//Import statements
Class className {
//methods
//attributes (private)
public static void main(String [] args) {
//statements
}//end main} //end class
4
Problem statement: Write a program that asks for the users
first, middle, and last names and
replies with their initials.
Example input: Andrew Lloyd
Weber
output: ALW
Identify the major tasks the program has to perform.
We need to know what to develop before we develop!
Tasks:
Get the users first, middle, and
last names
Extract the initials and create
the monogram
Output the monogram
8
Development Steps
We will develop this program in
two steps:
1. Start with the program template and add code to get input
2.
Add code to compute and
display the monogram
9
Step 1 Design
The program specification states
get the users name but doesnt
say how.
We will consider how in the
Step 1 code
10
We will use Scanner for input
Input Style Choice #1
Input first, middle, and last names
separately
Input Style Choice #2 Input the
full name at once
11
We choose Style #2 because it
is easier and quicker for the user
to enter the information
12
Scanner reads input from stdin.
Similar to scanf in C, but no
need for conversions.
13
Step 1 Code
import java.util.*; //Scanner reads data from std in
class Monogram {
public static void main (String[ ] args) {
String name;
Scanner in = new Scanner(System.in);
System.out.print("Enter Your Name: ");
name = in.nextLine(); //reads a line in delimiter = \n
System.out.println(name);
}
}
14
Step 1 Test
In the testing phase we run the
program and verify that we can
enter the name and the name
we enter is displayed correctly.
15
Examples
James Marshall Hendrix (okay)
Finn, Huckleberry (not okay)
John Kennedy (okay or not okay)
Is John Kennedy wrong, just because he doesnt use his middle
name (JFK)?
16
Step 2 Design
Our programming skills are limited, so we will make the following assumptions:
1. input string contains first,
middle, and last names
2. first, middle, and last names
are separated by single blank spaces
17
Step 2 Design continued
Given the valid input, we can
compute the monogram by
1. breaking the input name into
first, middle, and last
18
2. extracting the first character
from each one
3. concatenating the characters
from step 2.
import java.util.*; //needed for Scanner class
class Monogram {
public static void main (String[ ] args) {
...
//Extract first, middle, and last names
int i = 0;
String first = name.substring(0, i = name.indexOf(" "));
String middle = name.substring(i + 1,
i = name.indexOf(" ", i + 1));
String last = name.substring(i + 1, name.length());
//Compute the monogram
monogram = first.substring(0, 1) + middle.substring(0, 1) +
last.substring(0,1);
System.out.println("Your monogram is " + monogram);
} }
19
In the testing phase, we run the
program and verify that, for all
valid input values, correct monograms are displayed.
20
We run the program numerous
times.
Seeing one correct an-
swer is not enough. We have to
try out many different types of
(valid) input values.
21
1. Which one is true?
A. A class is an instance of an
object.
B. An object is an instance of
class
22
2. Which is correct?
For encapsulation,
A. private methods are usually declared.
B. public methods are usually declared.
C. private instance fields are usually declared.
D. public instance methods are usually declared.
23
3. True or False
Each class in java must contain
a main method.
24
Homework
Read Wu pages 52 - 76
Problems Chapter 2 Wu #20,
26, 29 (galapagos class will be
posted on course website), 32
25
Classpath
The class path tells the Java SDK
tools and applications where to
find third-party and user-defined
classes.
26
That is, classes that are not Java
extensions or part of the Java
platform.
The class path needs to find any
classes youve compiled with the
javac compiler. The javac compiler default is to look in the current directory to find classes.
27
The Java SDK find classes by
searching the Java platform (bootstrap) classes, any extension classes,
and the class path, in that order.
28
Extension classes, also known as
optional classes, are JAR files in
the directory
lib/ext [in the Java 2 Runtime
Environment]
jre/lib/ext [in the Java 2 SDK]
29
JAR Files
Java Archive Format files (jar)
are files that bundle and compress multiple files into one file.
A lot like tar.
30
Jar files are used to distribute
java packages.
Java packages
are collection of classes with a
common namespace (i.e import
packagename).
31
Using classpath on the javac compiler
Unix (use colon)
javac -cp .:<path to jar> <Class>.java
Windows (use semi-colon)
javac -cp .;<path to jar> <Class>.java
32
Must use class path with java
command as well
Unix (use colon)
java -cp .:<path to jar> <Class>
Windows (use semi-colon)
java -cp .;<path to jar> <Class>.java
33
Make sure to include current directory .
Once you use -cp option have
to be explicit with location of all
class paths (if you dont use -cp,
the default is to search current
directory for classes)
34
Example downloaded Galapagos package and placed in subdirectory of current directory where
source code file Triangle.java is
located.
javac -cp .:Galapagos/galapagos.jar Triangle.java
java -cp .:Galapagos/galapagos.jar Triangle
35
In java everything is a class. In
Turtle class there is a method
public void penColor(java.awt.Color color)
which will set the pen color. How
do you change colors?
36
Use javadoc to find if there is
any method of the java.awt.Color
class that returns a Color object.
37
Looking at javadocs you will find
that
static Color getColor(String nm)
returns a Color object.
38
How to get a Color?
java.awt.Color.getColor("blue");
will return the color blue
39
Can shorten with the use of
import java.awt.*;
...
Color.getColor("blue");
40