6 2 Introduction 6 3 In project 1 you ll be working extensively with strings If you do the extra credit you ll also be working with files Intro to Programming II Therefore it s useful to take a bit of time to remember how to This mean that they have a set of methods they respond to compareTo equals work with strings Strings and Files indexOf length replace startsWith endsWith Chris Brooks Department of Computer Science etc University of San Francisco Department of Computer Science University of San Francisco p 1 6 4 Strings in Java Strings in Java are objects Overloaded operators Department of Computer Science University of San Francisco p 2 6 5 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 is redifined to provide different functionality Strings are immutable Department of Computer Science University of San Fra 6 6 Strings are immutable This means that once a string is created it can t be changed To change it you need to create a copy String s1 hello world Iterating over strings To find the character at a particular location use charAt int index String s1 I love Java for int i 0 i s1 length i System out println s1 charAt i To change hello to goodbye we d need to do String s2 s1 replace hello goodbye s2 is goodbye world s1 is unchanged Department of Computer Science University of San Francisco p 4 Department of Computer Science University of San Francisco p 5 Department of Computer Science University of San Fra 6 7 String practice 6 8 Write a program that Reads in a string from System in Iterates over the string and prints out all the vowels String equality 6 9 To test whether two strings have the same contents use equals or equalsIgnoreCase String practice Write a program that will Read a string in from System in will test for object equality String s1 foo Print out the first word in the string 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 if s1 comes after s2 Department of Computer Science University of San Francisco p 7 6 10 Using Scanner to read from files Department of Computer Science University of San Francisco p 8 6 11 We can use the Scanner class to read from a file instead of System in String practice 6 12 Read in the file studentfile and print out all names beginning with a try 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 Department of Computer Science University of San Fra Read in the file studentfile and print out all people with first names of longer than 5 letters Building Project 1 The first class to consider is the Token class Two instance variables type value Read in the file studentfile and print out all people whose Last name comes after jones in the alphabet class variables for each type setters and getters toString plus a unit test Department of Computer Science University of San Francisco p 11 Department of Computer Science University of San Fran
View Full Document
Unlocking...