Question String Character access.docx - Question String Character access import java.util.Scanner public class WordScramble public static void

Question String Character access.docx - Question String...

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

Question: String Character access import java . util . Scanner ; public class WordScramble { public static void main ( String [] args ) { Scanner scnr = new Scanner ( System . in ) ; String userWord ; System . out . print ( "Enter a word with 5 letters: " ) ; userWord = scnr . next () ; System . out . print ( "Scrambled: " ) ; System . out . print ( userWord . charAt ( 3 )) ; System . out . print ( userWord . charAt ( 1 )) ; System . out . print ( userWord . charAt ( 4 )) ; System . out . print ( userWord . charAt ( 0 )) ; System . out . print ( userWord . charAt ( 2 )) ; System . out . println () ; } } Working with the end of a string Determining the last character in a string is often useful. If a string's length is known, the last character is at index length - 1. Ex: "Hey" has length 3, with y at index 2. The method s1. length() returns s1's length. Ex: If s1 is "Hey", s1.length() returns 3. A common task is to append (add to the end) a string to an existing string. The + operator can return a new string that appends a string to another string. Ex: If s is "Hey", then s + "!!!" returns "Hey!!!".
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read all 3 pages?

  • Summer '18
  • The Return, ASCII, Void type, Carriage return

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture