English subtitles

← We Have Intercepted A Secret Message - Intro to Java Programming

Get Embed Code
6 Languages

Showing Revision 6 created 05/25/2016 by Udacity Robot.

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