1. Home
  2. Computing & Technology
  3. Java
photo of Paul Leahy
Paul's Java Blog

By Paul Leahy, About.com Guide to Java

Answer to Monday's Encoded Line Question

Sunday October 5, 2008

The question on Monday was concerned with writing a program to decode this first line from a book by Iain Banks:

xv fpu vot ipk nk chpwindvoth tqeyditi

which turns out to be from The Crow Road:

it was the day my grandmother exploded

The program needed to take an encoded line from a user, use a combination of a normal for loop and switch statement, and finally display the result. I have to say that even though it was somewhat unexpected, the solution in c++ was most welcome. Well done to everyone who decoded the line.

Here's my version:

import java.util.Scanner;

public class Decoder {

  public static void main(String[] args) {

    //Get the input from the user
    Scanner input = new Scanner(System.in);
    System.out.println("Enter in text to be decoded: ");
    String codedText = input.nextLine();

    //Make sure the line is in lowercase
    codedText = codedText.toLowerCase();

    //Split the string into an array of chars
    char letters[] = codedText.toCharArray();

    //Decode the line of text
    char decodedLetter = ' ';
    for (int i=0; i     {
      switch(letters[i])
      {
        case 'a':
          decodedLetter = 'z';
          break;
        case 'b':
          decodedLetter = 'j';
          break;
        case 'c':
          decodedLetter = 'g';
          break;
        case 'd':
          decodedLetter = 'o';
          break;
        case 'e':
          decodedLetter = 'p';
          break;
        case 'f':
          decodedLetter = 'w';
          break;
        case 'g':
          decodedLetter = 'f';
          break;
        case 'h':
          decodedLetter = 'r';
          break;
        case 'i':
          decodedLetter = 'd';
          break;
        case 'j':
          decodedLetter = 'c';
          break;
        case 'k':
          decodedLetter = 'y';
          break;
        case 'l':
          decodedLetter = 'u';
          break;
        case 'm':
          decodedLetter = 'q';
          break;
        case 'n':
          decodedLetter = 'm';
          break;
        case 'o':
          decodedLetter = 'h';
          break;
        case 'p':
          decodedLetter = 'a';
          break;
        case 'q':
          decodedLetter = 'x';
          break;
        case 'r':
          decodedLetter = 'b';
          break;
        case 's':
          decodedLetter = 'k';
          break;
        case 't':
          decodedLetter = 'e';
          break;
        case 'u':
          decodedLetter = 's';
          break;
        case 'v':
          decodedLetter = 't';
          break;
        case 'w':
          decodedLetter = 'n';
          break;
        case 'x':
          decodedLetter = 'i';
          break;
        case 'y':
          decodedLetter = 'l';
          break;
        case 'z':
          decodedLetter = 'v';
          break;
        default:
          decodedLetter = letters[i];
          break;
      }
      System.out.print(decodedLetter);
    }
  }
}

Comments

No comments yet. Leave a Comment

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss
Community Forum
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

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

All rights reserved.