1. Home
  2. Computing & Technology
  3. Java

Uncoded Line

By Paul Leahy, About.com

Level: Beginner

Focus: Loops, Switch statement, Scanner object

Uncoded Line Question

You might have written the decoding program in the encoded line exercise. It would seem silly not to make the encoder that goes with it.

The encoder follows the same principles as the decoder, except I want to highlight the difference between using a normal for loop and a "for-each" loop. Your program should take a line of text entered by the user, use a combination of a for-each loop and switch statement to encode it, and finally display the encoded line to the user.

As shown in the table below, the encoding program uses the same letter switch as the decoder. For example, the name "bob" becomes "rdr" (i.e., b=r, o=d, b=r). Don't worry about capital letters, just assume that all of the text is in lowercase.

The question is can your program encode this line?

it was a dark and stormy night

To get the most out of this question try and figure out the answer before coming back to read the solution on the next page.

Letter Switch
Decodedabcdefghijklm
Encodedprjitgcoxbsyn
Decodednopqr stuvwx yz
Encodedwdemh uvlzfq ka
Explore Java
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Java
  4. Programming Exercises
  5. Beginner Level
  6. Uncoded Line>

©2009 About.com, a part of The New York Times Company.

All rights reserved.