Download Java String Methods and Operations and more Slides Network security in PDF only on Docsity!
- Objectives
- utilize some useful Java libraries
- e.g. String, Scanner, HashMap, and Random
6. Using Libraries
Topics
1. The String Class
2. A Technical Support System
3. The InputReader Class
4. Reading Input with Scanner
5. Designing the Responder Class
6. Maps
7. Making Random Numbers
8. The Responder Class
9. Writing Class Docs for Users
Creating a String Object
String color = "blue";
String s1 = new String("hello ");
char chs[] = {‘a’, ‘n’, ‘d’, ‘y’}; String s2 = new String(chs);
String s3 = s1 + s2 + " davison"; // + is string concatenation
Four different ways (there are more). 1
2
"hello "
s
Testing Strings for Equality
- s1.equals(s2)
- lexicographical (dictionary) comparison
- returns true if s1 and s2 contain the same text
- s1 == s
- returns true if s1 and s2 refer to the same object
• Strings should always be compared with
equals().
continued Docsity.com
Comparing Strings
- s1.compareTo(s2)
- returns 0 if s1 and s2 are equal
- returns < 0 if s1 < s2; > 0 if s1 > s
- s1.startsWith("text")
- returns true if s1 starts with “text”
- s1.endsWith("text")
- returns true if s1 ends with “text”
Locating Things in Strings
- s1.indexOf('c')
- returns index position of first ‘c’ in s1, otherwise -
- s1.lastIndexOf('c')
- returns index position of last ‘c’ in s1, otherwise -
• Both of these can also take string arguments:
for text analysis
Changing Strings
- s1.replace('a', 'd')
- return new String object; replace every ‘a’ by ‘d’
- s1.toLowerCase()
- return new String object where every char has
been converted to lowercase
- s1.trim()
- return new String object where any white space
before or after the s1 text has been removed
How do you Change a String?
• Any change to a String object creates a new
object, but this can be assigned back to the
existing String variable.
String w = "foo"; String newW = w + "bar"; w = newW;
or
String w = "foo"; w = w + "bar";
"foo"
w
Strings and Arrays
String[] msgs = new String[2];
msgs[0] = "hello";
msgs[1] = new String("hi");
String t = msgs[1];
t.toLowerCase();
msgs[1].toLowerCase();
t = msgs[1].toLowerCase();
What is built?
What is changed?
2. A Technical Support System
• A technical support system
- users can describe their software problems and
get advice instantly!
• It read input from the user a line at a time,
and generates 'suitable' responses.
• The idea is based on the Eliza system,
developed by Joseph Weizenbaum.
What Classes and Operations?
• Classes:
- a top-level class: SupportSystem
- it will execute a input-response loop
- a class for obtaining input: InputReader
- a class for generating responses: Responder
Input
Processing
Response
InputReader
Responder
SupportSystem
- Operations
- SupportSystem needs to ask the InputReader for
the user's input line.
- SupportSystem will have to pass the input line to
the Responder to generate a response.
Information Hiding
• Know what an object can do,
not how it does it.
• Information hiding increases the level of
class independence.
• Class independence simplifies
the building of large systems
and their maintenance.
SupportSystem Processing Loop
printWelcome();
String line, response boolean finished = false; while (!finished) { line = inputReader.getInput( ); if (line.startsWith("bye")) // time to stop finished = true; else { response = responder.genResponse (line) System.out.println( response ); } } printGoodbye();
Input
Processing and Response
InputReader
Responder