Return to Video

We Have Intercepted A Secret Message - Intro to Java Programming

  • 0:01 - 0:05
    First, let's look at how we're going to use the decode method. It's going to
  • 0:05 - 0:10
    return a string with the decoded message. And it doesn't need any arguments.
  • 0:10 - 0:19
    So, in the decoder tester, we want to print out the result of decoder.decode.
  • 0:19 - 0:24
    The decoder was made with the coded messages string already in it. So we don't
  • 0:24 - 0:28
    need to pass that string in again. Now, back to the decoder itself. We need to
  • 0:28 - 0:34
    write a for loop that reads every 10th letter, starting with the 0th one. Since
  • 0:34 - 0:36
    we're reading a string, we want to go as long as the index is less than the
  • 0:36 - 0:41
    length of the string. EncodedMessage is the string that we're reading. And
  • 0:41 - 0:45
    instead of incrementing by one at a time I'm going to increment i by 10 every
  • 0:45 - 0:52
    time, so that we only see every tenth letter. Inside the loop, the part that I
  • 0:52 - 0:58
    want to repeat is reading the ith letter, and adding it to decoded message. So
  • 0:58 - 1:03
    decoded message will be itself Plus, encodedMessage.substring from i to i plus
  • 1:03 - 1:09
    1. Remember, this is how we got one letter out of a longer string. Let's see if
  • 1:09 - 1:13
    this works. Looks like there are more secret messages in here. If you're
  • 1:13 - 1:15
    curious what they are, I guess you'll have to try starting at different
  • 1:15 - 1:19
    indices. Let me talk for a moment about a couple of bugs that you might have
  • 1:19 - 1:24
    encountered. If you accidentally wrote less than or equals to, right here like
  • 1:24 - 1:29
    this. You might encounter a string out of bounds exception. When you try to
  • 1:29 - 1:34
    access too high of an index in encodedMessage. A string index out of bounds
  • 1:34 - 1:38
    exception is a runtime error. That particular runtime error is nice, because it
  • 1:38 - 1:42
    stops the program and tells you, you tried to access a value that doesn't
  • 1:42 - 1:47
    exist. Which is much better than silently messing up your program's result. You
  • 1:47 - 1:51
    could correctly, though, a little clumsily write the condition as, i is less
  • 1:51 - 1:56
    than or equal to encodedMessage.length(), minus 1. That would work, but I don't
  • 1:56 - 2:01
    think it's as clean. When you iterate over a string, it's best, to go, as long,
  • 2:01 - 2:05
    as i, is less than, the length.
Title:
We Have Intercepted A Secret Message - Intro to Java Programming
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
cs046: Intro to Programming
Duration:
02:05

English subtitles

Revisions Compare revisions