3.8. String access operations.pdf - 3.8 String access operations You have unveri ed email(s Please click on your name in the top right corner and browse

3.8. String access operations.pdf - 3.8 String access...

This preview shows page 1 - 3 out of 7 pages.

7/16/20193.8. String access operations1/7You have unveri±ed email(s). Please click on your name in the topright corner and browse to your pro±le to send another veri±cationemail.3.8 String access operationsA string is a sequence of characters in memory. Each string character has a position numbercalled an index. The numbering starts with 0, not 1.charAt(): The notation someString.charAt(0) determines the character at a particular index of astring, in this case index 0.Figure 3.8.1: String character access.importjava.util.Scanner; publicclassWordScramble { publicstaticvoid main(String[] args) { Scanner scnr = newScanner(System.in); StringusrWord = ""; System.out.print("Enter a word with 5 letters: "); usrWord = scnr.next(); System.out.println("Size: " + usrWord.length()); // Note: Error if usrWord has < 5 letters System.out.println("Original: " + usrWord); System.out.print("Scrambled: "); System.out.print(usrWord.charAt(3)); System.out.print(usrWord.charAt(4)); System.out.print(usrWord.charAt(1)); System.out.print(usrWord.charAt(0)); System.out.println(usrWord.charAt(2)); return; }}Enter a word with 5 letters: Stars Size: 5 Original: Stars Scrambled: rstSaPARTICIPATIONACTIVITY3.8.1: String access.Feedback?
Background image
7/16/20193.8. String access operations2/7Given userText is "Think". Do not type quotes in your answers.1)How many numbers do yousee: 0 1 2 342)What character is at index 1 ofuserText?3)What is the index of the lastcharacter, 'k', in userText?4)To what character does thisevaluate: userText.charAt(3)nThe String data type comes with several useful features. The features are made possible due toString's implementation as a class
Background image
Image of page 3

You've reached the end of your free preview.

Want to read all 7 pages?

  • Spring '16
  • Dang, 2034, 2 79 k

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture