110 likes | 232 Views
Strings – Example 8. public String typeTextWithCapsAndCorrection(String delimiter) { boolean Caps = false; String key, text ; clearDisplay() ; text = ""; key = waitForKeyPress(); while(key.compareToIgnoreCase(delimiter) != 0) {
E N D
Strings – Example 8 public String typeTextWithCapsAndCorrection(String delimiter) { boolean Caps = false; String key, text ; clearDisplay() ; text = ""; key = waitForKeyPress(); while(key.compareToIgnoreCase(delimiter) != 0) { if(key.equalsIgnoreCase("#")) { if(Caps == true) { Caps = false ; } else { Caps = true ; } } else { if(key.equalsIgnoreCase("Backspace")) { if(text.length() > 0) { text = text.substring(0,text.length() - 1) ; } } else { if(Caps == true) { text = text + key.toUpperCase(); } else { text = text + key.toLowerCase(); } } setDisplay(text); } key = waitForKeyPress(); } return text ; }
New javaphone version • In the new version of the javaphone the following facilities have been included • Battery always full charged by constructor • Signal level always full signal • Text typed on the screen wraps to multiple lines when necessary • You can include “\n” in text being displayed on the screen to move on to the next display line • The method setPrompt(String s) can be used to display a prompt on the bottom line of the screen. This does not interfere with text being typed by the user. For example • setPrompt(“Enter message”) displays “Enter message” in the prompt area • setPrompt(“”) effectively clears the prompt area
String class methods • Within a string, locating the position of a character or a portion of text is possible using one of following • indexOf(string) finds the first occurrence of the string (if there is one) starting from position zero • indexOf(string, startPos) finds the first occurrence of the string (if there is one) starting from position startPos • Both these methods return -1 if the string cannot be located. • What do you think lastIndexOf(string) and lastIndexOf(string,startPos) do?
String class methods • Four other useful methods are • startsWith(prefix) returns true if the string starts with the prefix • startsWith(prefix, startPos) returns true if the string starts with the prefix from position startPos • endsWith(suffix) returns true if the string ends with the suffix • replace(oldstring,newstring)) repalces all occurrances of oldString with newStringtrue if the string ends with the suffix
Strings – Example 9 public void split(String phrase) { String word ; int startPos, spacePos ; phrase = phrase + " " ; startPos = 0; spacePos = phrase.indexOf(" ",startPos) ; while(spacePos != -1) { word = phrase.substring(startPos,spacePos) ; addToDisplay(word + "\n") ; // display word and move to next line startPos = spacePos + 1 ; spacePos = phrase.indexOf(" ",startPos) ; } }
Strings – Example 10 public void usingTypeTextV1() { clearDisplay() ; String text ; text = typeTextWithCapsAndCorrection("enter"); // add two blank lines to the output/display addToDisplay("\n\n") ; split(text) ; }
Strings – Example 10 public void split(String phrase) { String word ; int startPos, spacePos ; phrase = phrase + " " ; startPos = 0; spacePos = phrase.indexOf(" ",startPos) ; while(spacePos != -1) { word = phrase.substring(startPos,spacePos) ; addToDisplay(word + "\n") ; // display word and move to next line startPos = spacePos + 1 ; spacePos = phrase.indexOf(" ",startPos) ; } }
Strings – Example 11 public void usingTypeTextV2() { String msgText, toNumber ; clearDisplay() ; setPrompt("Message") ; msgText = typeTextWithCapsAndCorrection("enter") ; clearDisplay() ; setPrompt("Number") ; toNumber = typeTextWithCapsAndCorrection("enter") ; setPrompt("") ; clearDisplay() ; sendText(toNumber, msgText) ; }
Strings – Example 12 public void translateEnglishToPirateV0() { String englishText, pirateText ; clearDisplay() ; englishText=typeTextWithCapsAndCorrection("enter"); pirateText = talkLikeAPirate(englishText) ; addToDisplay(" -- TRANSLATION -- " + pirateText) ; }
Pirate Talk • god my classmates are clever • arrr by thunder me shipmates are smart as paint arrr • how long have you been learning the java programming language • arrr how long have ye been learning th' Tall Ship seafarin' clipper arrr • my program crashed because of a bug • arrr me vessel is marooned because of a mutiny arrr • i used to be a c++ programmer • arrr i used to be a son of a biscuit eater milksop arrr • madam insulting the lecturer will not help you in the repeat exam • arrr proud beauty bilge-sucking th' yellow-bellied sapsucker will not help ye in th' walk the blank hempen halter arrr