Strings.

Slides:



Advertisements
Παρόμοιες παρουσιάσεις
Ancient Greek for Everyone: A New Digital Resource for Beginning Greek Unit 4: Conjunctions 2013 edition Wilfred E. Major
Advertisements

Προβολή SPmC TURBOHALER ΑΣθΜΑ ΧΑΠ Subordinated pages Animation step Structure of the pages is clear No animation Simple animation.
What is the definition of differentiation?. Differentiation is an approach to teaching that attempts to ensure that all students learn well, despite their.
Αναλογική- Ψηφιακή Φωτογραφία (Φιλμ-Αισθητήρας)
1 Please include the following information on this slide: Παρακαλώ, συμπεριλάβετε τις παρακάτω πληροφoρίες στη διαφάνεια: Name Balafouti MariaWhich of.
1 Basic network tools Layers recap Basic Addressing ping traceroute ipconfig.
ΘΕΩΡΙΑ ΔΙΑΛΕΞΗ 4 Αριθμητικές εκφράσεις και πράξεις Εντολές ανάθεσης
ΣYMBOΛIKOΣ ΥΠΟΛΟΓΙΣΜΟΣ. ΣYMBOΛIKOΣ ΥΠΟΛΟΓΙΣΜΟΣ - Παράδειγμα %polynomial (Expression, Variable) polynomial (X, X). polynomial (Term, X) :- number (Term).
CCS SysTheo The Church – The Parts. The Trinity and the Church The Roles Father – the Maker, Giver and Keeper John 17 John 15:1-17 Son – the Head Eph.
Ancient Greek for Everyone: Unit 3: Greek Nouns supplement: Tips on Translating Greek into English GREK 1001 Fall 2013 M-Th 9:30-10:20 Coates 236 Wilfred.
Εισαγωγή στον Προγραμματισμό, Αντώνιος Συμβώνης, ΣΕΜΦΕ, ΕΜΠ, Slide 1 Εβδομάδα 3: Υλοποίηση μεθόδων.
2013 edition Wilfred E. Major
Business Process Management and Knowledge Toolkit
TEMPLATES, STL ΠΡΟΓΡΑΜΜΑΤΑ ΜΕ ΠΟΛΛΑ ΑΡΧΕΙΑ. ΑΝΑΚΕΦΑΛΑΙΩΣΗ.
Θεωρία Γραφημάτων Θεμελιώσεις-Αλγόριθμοι-Εφαρμογές
Τεχνολογία ΛογισμικούSlide 1 Έλεγχος Καταψύκτη (Ada) Τεχνολογία ΛογισμικούSlide 39 with Pump, Temperature_dial, Sensor, Globals, Alarm; use Globals ; procedure.
Προγραμματισμός PASCAL Πληροφορική Γ' Λυκείου μέρος γ
Ασκηση NextGen POS. Ι. Δεληγιάννης, Τμ. Πληροφορικής ΤΕΙ-Θ UNIFIED PROCESS - ΑΝΑΛΥΣΗ2.
Αντικειμενοστρεφής Προγραμματισμός Κλάσεις και άλλα θέματα Απόστολος Ζάρρας * βασισμένα και σε δουλειά του.
Σχεδίαση-Ανάπτυξη Εφαρμογών Πληροφορικής Αντώνιος Συμβώνης, ΕΜΠ, Slide 1 Week 4: Exceptions Εβδομάδα 4: Εξαιρέσεις [Exceptions]
Πληροφοριακά Συστήματα και Βάσεις Δεδομένων
© GfK 2012 | Title of presentation | DD. Month
Εισαγωγικό Φροντηστήριο Διαχείριση Περιεχομένου Παγκόσμιου Ιστού και Γλωσσικά Eργαλεία.
Εισαγωγή στον Προγραμματισμό, Αντώνιος Συμβώνης, ΣΕΜΦΕ, ΕΜΠ, Slide 1 Εβδομάδα 7: Συμβολοσειρές.
ΤΕΧΝΙΚΕΣ Αντικειμενοστραφουσ προγραμματισμου
Προγραμματισμός ΙΙ Διάλεξη #7: Περισσότερες Δομές Ελέγχου Δρ. Νικ. Λιόλιος.
ΛΙΠΙΔΙΑ Ορισμός A group of compounds that are soluble in certain organic solvents such as Ether, Benzene, and Chloroform, and are insoluble in water.
Lesson 14: Around the city JSIS E 111: Elementary Modern Greek Sample of modern Greek alphabet, M. Adiputra,
Lesson 3a: Basic expressions JSIS E 111: Elementary Modern Greek Sample of modern Greek alphabet, M. Adiputra,
Comenius E.R.G.O Equal Rights Great Opportunities Presentation by the Greek Delegation to Hungary.
1/6/2012 FASTER LOGO FASTER ICT – Financial and Accounting Seminars Targeting European Regions You are expected to prepare a ppt presentation for each.
Lesson 18c: At the University JSIS E 111: Elementary Modern Greek Sample of modern Greek alphabet, M. Adiputra,
Προγραμματισμός ΙΙ Διάλεξη #6: Απλές Δομές Ελέγχου Δρ. Νικ. Λιόλιος.
Lesson 47-48a: Furniture JSIS E 111: Elementary Modern Greek Sample of modern Greek alphabet, M. Adiputra,
1 Please include the following information on this slide: Παρακαλώ, συμπεριλάβετε τις παρακάτω πληροφoρίες στη διαφάνεια: Name Giannakodimou Aliki Kourkouta.
Σοφία Τζελέπη, App Inventor ΜΕΡΟΣ B’ Σοφία Τζελέπη,
Developing Human Values Through the Cross-curricular Approach.
Δομές Δεδομένων 1 Στοίβα. Δομές Δεδομένων 2 Στοίβα (stack)  Δομή τύπου LIFO: Last In - First Out (τελευταία εισαγωγή – πρώτη εξαγωγή)  Περιορισμένος.
MARIE CURIE  Project about Project  Πειραματικό Λύκειο Πανεπιστημίου Μακεδονίας  Team 3 Ξενίδης Γιώργος Βαρελτζίδου Μαρίνα Γαβριηλίδου Ελένη.
1 Τμήμα Μηχανικών Ηλεκτρονικών Υπολογιστών και Πληροφορικής Πανεπιστήμιο Πατρών ΟΝΤΟΚΕΝΤΡΙΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΙΙ (C++) Δείκτες και Συμβολοσειρές (Pointers.
6 Η ΠΑΡΟΥΣΙΑΣΗ: ΠΑΝΤΕΙΟ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΟΙΝΩΝΙΚΩΝ ΚΑΙ ΠΟΛΙΤΙΚΩΝ ΕΠΙΣΤΗΜΩΝ ΤΜΗΜΑ: ΕΠΙΚΟΙΝΩΝΙΑΣ, ΜΕΣΩΝ ΚΑΙ ΠΟΛΙΤΙΣΜΟΥ ΜΑΘΗΜΑ: ΕΙΣΑΓΩΓΗ ΣΤΗ ΔΙΑΦΗΜΙΣΗ.
Προγραμματισμός ΙΙ Διάλεξη #5: Εντολές Ανάθεσης Εντολές Συνθήκης Δρ. Νικ. Λιόλιος.
1 Τμήμα Μηχανικών Ηλεκτρονικών Υπολογιστών και Πληροφορικής Πανεπιστήμιο Πατρών ΟΝΤΟΚΕΝΤΡΙΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΙΙ (C++) Κληρονομικότητα.
“ Ἡ ἀ γάπη ἀ νυπόκριτος. ἀ ποστυγο ῦ ντες τ ὸ πονηρόν, κολλώμενοι τ ῷ ἀ γαθ ῷ, τ ῇ φιλαδελφί ᾳ ε ἰ ς ἀ λλήλους φιλόστοργοι, τ ῇ τιμ ῇ ἀ λλήλους προηγούμενοι.
Δρ. Μαρία Ι. Ανδρέου Εισαγωγή στον Αντικειμενόστρεφη Προγραμματισμό (Object-Oriented Programming) Java Syntax and Style.
ΗΥ Παπαευσταθίου Γιάννης1 Clock generation.
Week 11 Quiz Sentence #2. The sentence. λαλο ῦ μεν ε ἰ δότες ὅ τι ὁ ἐ γείρας τ ὸ ν κύριον Ἰ ησο ῦ ν κα ὶ ἡ μ ᾶ ς σ ὺ ν Ἰ ησο ῦ ἐ γερε ῖ κα ὶ παραστήσει.
Κωδικός Θ: ΤΠ4003, Κωδικός Ε: ΤΠ4103 (ΜΕΥ/Υ) Ώρες (Θ - ΑΠ - Ε): Προαπαιτούμενα: ΤΠ2003,2103.
Ψηφιακά Παιχνίδια και μάθηση Δρ. Νικολέτα Γιαννούτσου Εργαστήριο Εκπαιδευτικής Τεχνολογίας.
Μαθαίνω με “υπότιτλους”
Εισαγωγή στον Προγ/μό Η/Υ
Εισαγωγή στον Προγ/μό Υπολογιστών
Αντικειμενοστραφής Προγραμματισμός ΙΙ
Αντικειμενοστραφής Προγραμματισμός ΙΙ
Εισαγωγή στον Προγ/μό Υπολογιστών
JSIS E 111: Elementary Modern Greek
Adjectives Introduction to Greek By Stephen Curto For Intro to Greek
(ALPHA BANK – EUROBANK – PIRAEUS BANK)
Example Rotary Motion Problems
2013 edition Wilfred E. Major
Find: φ σ3 = 400 [lb/ft2] CD test Δσ = 1,000 [lb/ft2] Sand 34˚ 36˚ 38˚
aka Mathematical Models and Applications
Cipher Feedback Mode Network Security.
Baggy Bounds checking by Akritidis, Costa, Castro, and Hand
Assignments, Expressions & Operators
Entry 27 – Starter Copy and simplify
Complements White Box Testing Finds a different class of errors
Inheritance and Polymorphism
More of What You Should Already Know
class news Miss Jackson’s A Note from the Teacher
Μεταγράφημα παρουσίασης:

Strings

Στόχοι: Μαθαίνουμε για literal strings Μαθαίνουμε για String constructors και commonly used methods Κατανοούμε immutability of strings Μαθαίνουμε να μετατρέπουμε convert strings σε αριθμούς και αριθμούς σε strings μαθαίνουμε several useful methods της Character class The char data type is not in the AP Java subset, while strings are. However, any reasonable course will teach chars. One of the reasons for excluding char from the subset was the perceived danger of confusing a char constant 'x' with a string constant "x". An unstated objective of this chapter is to get a clear understanding that char and String are two different data types. char is a primitive data type; strings are objects. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

The String class Ένα object της String class αναπαριστά string από characters. Το String class ανήκει στο java.lang package, το οποίο κάνει built into Java. Όπως και σε άλλες classes, String έχουν constructors και methods. Σε διαφορά με other classes, String έχουν δυο operators, τους + και += (used for concatenation). There is no need to import java.lang.String because the java.lang package is imported automatically into all classes. The + and += operators for strings are an exception in Java, the only example of overloaded operators for objects. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Literal Strings Literal strings είναι anonymous constant objects του String class που ορίζονται σαν text in double quotes. το string text μπορεί να περιλαμβάνει “escape” characters. Για παράδειγμα: \\ stands for \ \n stands for the newline character \" stands for the double quote character. Like the char type, String objects internally represent characters using Unicode. "Biology”, "C:\\jdk1.4\\docs”, "Hello\n" Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Literal Strings (συνέχ.) ΔΕΝ πρέπει να κατασκευαστούν: απλά υπάρχουν εκεί (they are “just there.”) Μπορούν να ανατεθούν σε String variables. can be passed to methods and constructors as arguments. have methods you can call: Calling a method of a literal string, as in "sometext".someMethod(...), may seem unusual to programmers who are used to other languages. "sometext".equals(str) is often used instead of str.equals("sometext") because the former version works even when str is null, while the latter would throw a null reference exception. String fileName = "fish.dat"; button = new JButton("Next slide"); if (”Start".equals(cmd)) ... Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Immutability Έτσι και δημιουργηθεί ένα string ΔΕΝ μπορεί να αλλάξει. ΚΑΜΙΑ από τις methods του δεν αλλάζει το string. Τέτοιου τύπου objects ονομάζονται immutable. Immutable objects μας εξυπηρετούν/διευκολίνουν (are convenient) επειδή two references can point to the same object με ασφάλεια. Δεν υπάρχει κίνδυνος να αλλάξει το object through one reference χωρίς να το γνωρίζουν οι άλλες. If all the instance fields in a class are private and none of the methods can directly or indirectly set the field values, then the class’s objects are immutable. A few String methods create and return a different string, but none of them changes the string for which they were called. The Integer, Character, and Double classes, discussed later, are immutable, too. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Less efficient and wasteful Immutability (συνέχ.) Πλεονεκτήματα: ΠΙΟ αποδοτικά, δεν υπάρχει ανάγκη αντιγραφής. String s1 = "Sun"; String s2 = s1; String s1 = "Sun"; String s2 = new String(s1); s1 s1 "Sun" "Sun" Thus it is safe for immutable objects to copy references rather than the contents. Cloning (creating copies of objects) is not required. This may save time when Strings are stored in lists, etc. s2 s2 "Sun" OK Less efficient and wasteful Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Immutability (συνέχ.) Μειονέκτημα: ΛΙΓΟΤΕΡΟ αποδοτικό — πρέπει να δημιουργήσουμε ένα ΝΕΟ string και να πετάξουμε το παλιό σε κάθε μικρή αλλαγή. String s = "sun"; char ch = Character.toUpper(s.charAt (0)); s = ch + s.substring (1); On the other hand, the immutability may result in tremendous inefficiency when strings are modified often, especially when a string is gradually built by appending characters or numbers to it. Programmers should use the StringBuffer class for these tasks. s "sun" 'S' + "un" Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Empty Strings ένα empty string ΔΕΝ έχει characters; Είναι of length 0. String s2 = new String(); Empty strings You may need an empty string as a starting point when you plan to append items to a string. private String errorMsg; errorMsg is null Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Constructors String’s no-args and copy constructors are not used much. Other constructors convert arrays into strings String s1 = new String (); String s2 = new String (s1); String s1 = ""; String s2 = s1; As explained earlier, it is a good idea to avoid the copy constructor. One useful constructor converts an array of chars into a String. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Methods — length, charAt Επιστρέφει τον αριθμό των characters μέσα στο string Επιστρέφει τον k-th char int length (); char charAt (k); Οι θέσεις των Character σε ένα strings αριθμούνται αρχίζοντας από το 0 Returns: Character positions in strings are counted starting from 0, so str.charAt(0) returns the first character and str.charAt(str.length() - 1) returns the last character. This is consistent with indices in arrays (Chapter 10): the index of the first element is 0. ”Flower".length(); ”Wind".charAt (2); 6 ’n' Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Methods — substring String s2 = s.substring (i, k); returns the substring of chars in positions from i to k-1 String s2 = s.substring (i); returns the substring from the i-th char to the end strawberry i k Returns: It is actually convenient that the k-th char is excluded from the substring. The length of the substring is k ‑ i. str.substring (i) is the same as str.substring(i, str.length()). ”strawberry".substring (2,5); "unhappy".substring (2); "emptiness".substring (9); ”raw" "happy" "" (empty string) Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Methods — Concatenation String result = s1 + s2; concatenates s1 and s2 String result = s1.concat (s2); the same as s1 + s2 result += s3; concatenates s3 to result result += num; converts num to String and concatenates it to result It looks like the += operator for strings violates immutability. In fact the string is not changed, rather the reference is reassigned. s1 += s2 is the same as s1 = s1 + s2. So if you have String s = "Nice ", t = s; s += "day"; the value of t remains "Nice ". Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Methods — Find (indexOf) 0 8 11 15 String date ="July 5, 2012 1:28:19 PM"; date.indexOf ('J'); 0 date.indexOf ('2'); 8 date.indexOf ("2012"); 8 date.indexOf ('2', 9); 11 date.indexOf ("2020"); -1 date.lastIndexOf ('2'); 15 Returns: (starts searching at position 9) String has four overloaded versions of indexOf and four versions of lastIndexOf. lastIndexOf(ch, fromPos) starts looking at fromPos and goes backwards towards the beginning of the string. (not found) Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Methods — Comparisons boolean b = s1.equals(s2); returns true if the string s1 is equal to s2 boolean b = s1.equalsIgnoreCase(s2); returns true if the string s1 matches s2, case-blind int diff = s1.compareTo(s2); returns the “difference” s1 - s2 int diff = s1.compareToIgnoreCase(s2); returns the “difference” s1 - s2, case-blind You cannot use relational operators for comparing the contents of strings. s1.compareTo(s2) returns an int. Basically if s1 is “smaller” than s2, the result is negative, and if s1 is “larger” the result is positive. compareTo returns 0 whenever equals returns true. Here is how Java docs describe compareTo: Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true. This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string — that is, the value: this.charAt(k)-anotherString.charAt(k) If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings — that is, the value: this.length()-anotherString.length() Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Methods — Replacements String s2 = s1.trim (); returns a new string formed from s1 by removing white space at both ends String s2 = s1.replace(oldCh, newCh); returns a new string formed from s1 by replacing all occurrences of oldCh with newCh String s2 = s1.toUpperCase(); String s2 = s1.toLowerCase(); returns a new string formed from s1 by converting its characters to upper (lower) case Note that these methods do not change the string s1 but create and return a new string. trim() only removes whitespace at the ends of the string, not in the middle. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Replacements (cont’d) Example: how to convert s1 to upper case A common bug: s1 = s1.toUpperCase(); s1 remains unchanged s1.toUpperCase(); s1.toUpperCase(); doesn’t do anything. The correct statement is: s1 = s1.toUpperCase(); The variable s1 is reassigned to the new string returned by s1.toUpperCase(); the old string is disposed of. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Methods — toString It is customary to provide a toString method for your class. toString converts an object into a String (for printing it out, for debugging, etc.). is the same as System.out.print (obj); The Object class (and therefore any object) has a toString method that returns a cryptic description of the object. It is a good idea to override this default method and define a more suitable one for your class. As the example shows, this method is used for implicit “casting” of objects of your class into strings. It works with the print and println methods and with the + operator for strings. System.out.print (obj.toString()); Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Numbers to Strings and Strings to Numbers Integer and Double are “wrapper” classes from java.lang that represent numbers as objects. Integer and Double provide useful static methods for conversions: String s1 = Integer.toString (i); String s2 = Double.toString (d); int n = Integer.parseInt (s1); double x = Double.parseDouble (s2); int i; double d; A wrapper class has a constructor that takes an item of the “wrapped” type as an argument. For example, Integer is a wrapper for int: int x = 5; Integer obj = new Integer(x); The wrapped type can be a class, too. The wrapper class adds run-time functionality to the wrapped object. In this case, Integer and Double just provide handy static methods for numbers. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Numbers to Strings Three ways to convert a number into a string: 1. String s = "" + num; 2. String s = Integer.toString (i); String s = Double.toString (d); 3. String s = String.valueOf (num); int i; double d; You can also convert a char to a string by using String s = "" + ch; By convention, a static method valueOf in a class converts something (its arguments) into an object of this class. For example: public class Fraction { public static Fraction valueOf(double x) {...} should take a double and return a corresponding Fraction. Here String.valueOf(x) returns a string. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Numbers to Strings (cont’d) The DecimalFormat class can be used for more controlled conversions of numbers into strings: import java.text.DecimalFormat; ... DecimalFormat money = new DecimalFormat("0.00"); double amt = …; String s = money.format (amt); 56.7899 As usual in Java, there is a hierarchy of subclasses and rather elaborate rules for specifying different formats. For example, you can define DecimalFormat dollars = new DecimalFormat("$#,##0.00"); Then System.out.println (dollars.format(12345678.987)); displays $12,345,678.99 "56.79" Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Strings to Numbers int n = Integer.parseInt(s); double x = Double.parseDouble(s); These methods throw a NumberFormatException if s does not represent a valid number. Older versions of SDK (before 1.2) did not have Double.parseDouble; you had to use In a real program you should use exception handling with number conversions to avoid aborting the program when the user mistypes a character: try { String str = input.readLine(); int x = Integer.parseInt(str); catch (NumberFormatException e) ... // show a friendly message that // an error has occurred and give // a chance to reenter } double x = Double.valueOf(s).doubleValue(); Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Character Methods java.lang.Character is a class that represents characters as objects. Character has several useful static methods that determine the type of a character. Character also has methods that convert a letter to the upper or lower case. Character is a wrapper class for char. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Character Methods (cont’d) if (Character.isDigit (ch)) ... .isLetter... .isLetterOrDigit... .isUpperCase... .isLowerCase... .isWhitespace... return true if ch belongs to the corresponding category Whitespace is space, tab, newline, etc. See Java docs for the formal definition of whitespace characters. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Character methods (cont’d) char ch2 = Character.toUpperCase (ch1); .toLowerCase (ch1); if ch1 is a letter, returns its upper (lower) case; otherwise returns ch1 int d = Character.digit (ch, radix); returns the int value of the digit ch in the given int radix char ch = Character.forDigit (d, radix); returns a char that represents int d in a given int radix For example Character.digit('7',10) returns 7 and Character.digit('A', 16) returns 10. Character.forDigit(7,10) returns '7'. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

StringTokenizer java.util.StringTokenizer is used to extract “tokens” from a string. Tokens are separated by delimiters (e.g., whitespace). A tokenizer object is constructed with a given string as an argument. The second optional argument is a string that lists all delimiters (default is whitespace). The StringTokenizer’s constructor takes a string, extracts all the tokens, and places them into a queue. Then the nextToken method retrieves and returns the next token from the queue. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

StringTokenizer (cont’d) import java.util.StringTokenizer; ... String str = input.readLine(); StringTokenizer q = new StringTokenizer (str); // or: // new StringTokenizer (str, ";+ \t, "); int n = q.countTokens (); while ( q.hasMoreTokens() ) { String word = q.nextToken(); Delimiters are whitespace All delimiters The number of found tokens You can either call the countTokens() method to find out how many tokens you have or call hasMoreTokens() to see if there are any tokens left in the queue. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

EquationSolver Applet This applet uses the same text field for input and for output. The initial version requires spaces around + and - signs. The version extended in the lab assignment can handle input with no spaces: it adds spaces around arithmetic operation signs. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

EquationSolver (cont’d) Uses many techniques discussed earlier: trim, substring, and other String methods clean the input, surround operation signs with spaces StringTokenizer extracts tokens Character.isDigit, and other Character methods identify tokens Integer.parseInt converts tokens to numbers String's + operator combines strings and numbers to generate output This lab and the end-of-chapter exercises give you a chance to get comfortable with String methods and number-to-string conversions. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Review: What makes the String class unusual? How can you include a double quote character into a literal string? Is "length".length() allowed syntax? If so, what is the returned value? Define immutable objects. Does immutability of Strings make Java more efficient or less efficient? What makes the String class unusual? + and += operators How can you include a double quote character into a literal string? \" Is "length".length() allowed syntax? If so, what is the returned value? Yes, it is valid and returns 6 Define immutable objects. Objects that cannot be changed by calling their methods (nor by directly manipulating their fields). Does immutability of strings make Java more efficient or less efficient? It depends. It may be more efficient if the code involves making many copies of strings. It is less efficient if the code involves changing characters in strings or assembling strings from separate items (chars or numbers). Actually the latter situation is more common. You should then use the StringBuffer class rather than String. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Review (cont’d): How do you declare an empty string? Why are String constructors not used very often? If the value of String city is "Boston", what is returned by city.charAt (2)? By city.substring (2, 4)? How come String doesn’t have a setCharAt method? Is s1 += s2 the same as s1 = s1 + s2 for strings? How do you declare an empty string? String s = ""; Why are String constructors not used very often? Because you can simply assign a literal string to a variable, so there is no need to make copies of strings. If the value of String city is "Boston", what is returned by city.charAt(2) ? By city.substring(2,4) ? 's' and "st", respectively. How come String doesn’t have a setCharAt method? To keep it immutable. StringBuffer has setCharAt(ch,pos). Is s1 += s2 the same as s1 = s1 + s2 for strings? Yes Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Review (cont’d): What do the indexOf methods do? Name a few overloaded versions. What is more efficient for strings: == and other relational operators or equals and compareTo methods? What does the trim method do? What does s.toUpperCase() do to s? What does the toString method return for a String object? What do the indexOf methods do? Name a few overloaded versions. Find a character or a substring in a string. indexOf(char), indexOf(String), indexOf(char, int fromPos), indexOf(String, int fromPos) What is more efficient for strings: == and other relational operators or equals and compareTo methods? This is not a matter of efficiency: you must use equals and compareTo to compare the contents of strings; == compares references, which are equal only if they refer to exactly the same string. What does the trim method do? It returns a new string which contains this string with whitespace removed at the ends. What does s.toUpperCase() do to s? Nothing: s is immutable. What does the toString method return for a String object? This string Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου

Review (cont’d): Name a simple way to convert a number into a string. Which class has a method for converting a String into an int? Name a few Character methods that help identify the category to which a given character belongs. What is the StringTokenizer class used for? Name a simple way to convert a number into a string "" + num; Which class has a method for converting a String into an int? Integer (the method parseInt) Name a few Character methods that help identify the category to which a given char belongs. isDigit, isLetter, isUpperCase, isLowerCase, isWhitespace. What is the StringTokenizer class used for? To extract tokens (contiguous blocks of characters) separated by delimiters. Εισαγωγή στο ΟΟΡ και Java Δρ. Μαρία Ι. Ανδρέου