Unformatted text preview:

{small lecturenumber - hepage :} Introduction{small lecturenumber - hepage :} Strings in Java{small lecturenumber - hepage :} Overloaded operators{small lecturenumber - hepage :} Strings are immutable{small lecturenumber - hepage :} Iterating over strings{small lecturenumber - hepage :} String practice{small lecturenumber - hepage :} String equality{small lecturenumber - hepage :} String practice{small lecturenumber - hepage :} Using Scanner to read from files{small lecturenumber - hepage :} String practice{small lecturenumber - hepage :} Building Project 1Intro to Programming IIStrings and FilesChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p. 1/??6-2: Introduction•In project 1, you’ll be working extensively with strings.◦If you do the extra credit, you’ll also be working with files.•Therefore, it’s useful to take a bit of time to remember how towork with strings.Department of Computer Science — University of San Francisco – p. 2/??6-3: Strings in Java•Strings in Java are objects•This mean that they have a set of methods they respond to:◦compareTo(), equals()◦indexOf()◦length()◦replace()◦startsWith(), endsWith()◦etcDepartment of Computer Science — University of San Francisco6-4: Overloaded operators•We can also use the ’+’ symbol to concatenate strings.◦String s1 = “hello”◦String s2 = “world”◦String s3 = s1 + s2 // s3 = “hello world”•This is a phenomenon called overloading; an operator isredifined to provide different functionality.Department of Computer Science — University of San Francisco – p. 4/??6-5: Strings are immutable•Strings are immutable◦This means that once a string is created, it can’t bechanged.◦To change it, you need to create a copy.•String s1 = “hello world”•To change “hello” to “goodbye”, we’d need to do:•String s2 = s1.replace(“hello”, “goodbye”);•s2 is “goodbye world”, s1 is unchangedDepartment of Computer Science — University of San Francisco – p. 5/??6-6: Iterating over strings•To find the character at a particular location, use charAt(intindex)String s1 = ‘‘I love Java’’for (int i = 0; i < s1.length(); i++) {System.out.println(s1.charAt(i));}Department of Computer Science — University of San Francisco6-7: String practice•Write a program that:◦Reads in a string from System.in◦Iterates over the string and prints out all the vowels.Department of Computer Science — University of San Francisco – p. 7/??6-8: String equality•To test whether two strings have the same contents, useequals() or equalsIgnoreCase();•== will test for object equality◦String s1 = “foo”;◦String s2 = “foo”;◦s1.equals(s2), but not s1 == s2•You can also use compareTo()◦Returns -1 if s1 comes before s2, 0 if they’re equal, and 1 ifs1 comes after s2.Department of Computer Science — University of San Francisco – p. 8/??6-9: String practice•Write a program that will:◦Read a string in from System.in;◦Print out the first word in the string.Department of Computer Science — University of San Francisco6-10: Using Scanner to read from files•We can use the Scanner class to read from a file instead ofSystem.intry {Scanner sc = new Scanner(new File("studentlist"));while (sc.hasNext()) {System.out.println(sc.next());}} catch(FileNotFoundException e) {System.out.println("File not found.");}Department of Computer Science — University of San Francisco – p. 10/??6-11: String practice•Read in the file “studentfile” and print out all names beginningwith ’a’.•Read in the file ’studentfile’ and print out all people with firstnames of longer than 5 letters.•Read in the file ’studentfile’ and print out all people whose Lastname comes after ’jones’ in the alphabet.Department of Computer Science — University of San Francisco – p. 11/??6-12: Building Project 1•The first class to consider is the Token class.•Two instance variables:◦type◦value•class variables for each type•setters and getters•toString(), plus a unit test.Department of Computer Science — University of San


View Full Document

USF CS 112 - Strings and Files

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download Strings and Files
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Strings and Files and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Strings and Files 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?