1 / 11

Strings – Example 8

Strings – Example 8. public String typeTextWithCapsAndCorrection(String delimiter) { boolean Caps = false; String key, text ; clearDisplay() ; text = ""; key = waitForKeyPress(); while(key.compareToIgnoreCase(delimiter) != 0) {

pules
Download Presentation

Strings – Example 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 ; }

  2. 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

  3. 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?

  4. 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

  5. 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) ; } }

  6. 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) ; }

  7. 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) ; } }

  8. 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) ; }

  9. Strings – Example 12 public void translateEnglishToPirateV0() { String englishText, pirateText ; clearDisplay() ; englishText=typeTextWithCapsAndCorrection("enter"); pirateText = talkLikeAPirate(englishText) ; addToDisplay(" -- TRANSLATION -- " + pirateText) ; }

  10. 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

More Related