secret messages Java

I am on lesson 13, and I am completely missing something... her cheats in the back of the book aren't helping much because it's only pieces of the code for each set of instructions.  Some instructions have you move previously written code, or delete previously written code,  and that is not visible in the cheats.

Bugger.  I'm going through the lesson and the cheats to see what the heck I missed.  The code is not changing the index position so I only get the coded message back in one letter.

Hello World,  comes back as  NNNNNNNNNNN

which means my else statement isn't executing either.  It should for the "_",  or anything that isn't in the alphabet.

So it's finding the index, and giving a code letter,  but then just using that code letter for every space in the original message....   Arggg.
I'll figure it out.  Then I'll update with where the mistake was.  This might take a bit.


Found it.... I figured it out without the book.  It was not changing the variables for the letter and letter position because I did not have them in the for{}.   Grrrr.  Will I ever figure this stuff out?
Wrong position edited with // red
Right position in blue.


package nellie.tobey.secretcode;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import nellie.tobey.mywindow.MyWindow;

public class SecretCode extends MyWindow {
 public SecretCode() {
  String fileName = "key.txt";
  //read the alphabet and key from the file
  try {
  BufferedReader in = new BufferedReader(new FileReader(new File(fileName)));
  String alphabet = in.readLine();
  String key = in.readLine();
  in.close();
  String messageToEncode = promptForString("Type your message to encode:");
  String encodedMessage= encode(messageToEncode, alphabet, key);
  print(encodedMessage);
  String messageToDecode = promptForString("Enter secret message");
  String decodedMessage= encode(messageToDecode, key, alphabet);
  print(decodedMessage);
  }

  catch(FileNotFoundException e) {
   print("Could not find file " + fileName);
  }
  catch(IOException e) {
   print("Could not open file: " + fileName);
  }
 }
 private String encode(String message, String fromAlphabet, String toAlphabet ) {
  String newMessage = "";
 // String letter = message.substring(0,1);
 // int letterPos = fromAlphabet.indexOf(letter);
  for( int i = 0; i<message.length(); i++) {
  String letter = message.substring(i,i+1);
  int letterPos = fromAlphabet.indexOf(letter);
  if (letterPos>-1) {

   letter = message.substring(i, i+1);
   String newLetter = toAlphabet.substring(letterPos, letterPos + 1);
   newMessage += newLetter;
  }
   else {
    newMessage += letter;
   }}

  return newMessage;

 }
 public static void main(String[] args) {
   new SecretCode();
 }
}

Comments

Popular posts from this blog

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame

JavaScript and a Matrix