Strings Example 8 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Strings Example 8

Description:

Strings Example 8. public String typeTextWithCapsAndCorrection(String delimiter) ... beauty bilge-sucking th' yellow-bellied sapsucker will not help ye in th' walk ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 12
Provided by: dermotshin
Category:

less

Transcript and Presenter's Notes

Title: Strings Example 8


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() gt 0)
  • text text.substring(0,text.l
    ength() - 1)

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,space
    Pos)
  • 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,space
    Pos)
  • 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("e
    nter")
  • clearDisplay()
  • setPrompt("Number")
  • toNumber typeTextWithCapsAndCorrection("
    enter")
  • setPrompt("")
  • clearDisplay()
  • sendText(toNumber, msgText)

9
Strings Example 12
  • public void translateEnglishToPirateV0()
  • String englishText, pirateText
  • clearDisplay()
  • englishTexttypeTextWithCapsAndCorrection("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

11
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com