8 Replies - 950 Views - Last Post: 23 March 2013 - 05:44 PM Rate Topic: -----

#1 chrisgoad3882  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-March 13

Cipher and Decipher

Posted 23 March 2013 - 02:08 PM

I am currently having lots of trouble with my computer science project. I keep getting hung up and currently cannot get passed this out-of-bounds exception. Please help. Rubric and project folder is attached.

/**
* This class tests CaesarShiftEncryption and CaesarShiftDecryption classes
*
* @author Christopher Goad
* @version 3-13-12
*/

import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.IOException;
import java.io.PrintWriter;
public class ChrisGoadCipherTester
{
    //constant for the alphabet
    public static ArrayList<Character> alphabet = makeAlphabet();
   
    public ChrisGoadCipherTester()
    {
    }
   
    public static ArrayList<Character> makeAlphabet()
    {
        ArrayList<Character> alphabet = new ArrayList<Character>();
        int letter = 65;
        for (int loop = 0; loop < 26; loop++)
        {
            alphabet.add((char)letter);
            letter++;
        }
        return alphabet;
    }
    
    public static void main(String[] args) throws IOException
    {
        Scanner in = new Scanner(System.in);
        //Menu for the user
        System.out.println("===============================");
        System.out.println("     [A] Chris Goad Cipher ");
        System.out.println("     [B] Chris Goad Decipher ");
        System.out.println("           [Q] Quit           ");
        System.out.println("===============================");
        System.out.print("Please select an letter option from the above menu and hit enter: ");
       
        String input = in.next();
        if (input.equalsIgnoreCase("A"))
        {
            ChrisGoadCipher.getInput();
        }
        else if (input.equalsIgnoreCase("B"))
        {
            ChrisGoadDecipher.getInput();
        }
        else if (input.equalsIgnoreCase("Q"))
        {
            System.out.println("You chose to quit. Goodbye!");
        }
        else
        {
            System.out.println("You did not choose a vaild option.");
        }
    }
}



/**
* This class is used to encrypt messages with a Caesar shift number.
*
* @author Christopher Goad
* @version 3-13-12
*/

import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.IOException;
import java.io.PrintWriter;
public class ChrisGoadCipher
{
    private static ArrayList<Character> newAlphabet = new ArrayList<Character>();
    private static ArrayList<Character> alphabet = ChrisGoadCipherTester.alphabet;
    
    public ChrisGoadCipher()
    {   
        
    }
   
    public static void getInput() throws IOException
    {
        //Ask for user input and call required methods
        Scanner in = new Scanner(System.in);
        createNewAlphabet();
        System.out.println("Here is the alphabet being used for encryption:");
        PrintWriter outA = new PrintWriter("cipherAlphabet.txt");
        displayNewAlphabet(newAlphabet);
        outA.println(newAlphabet);
        outA.close();
        System.out.print("\nEnter the phrase that will be encrypted: ");
        String message = in.nextLine();
        String newMessage = encrypt(message);
        System.out.println("Here is the encrypted message using the cipher alphabet: ");
        PrintWriter outM = new PrintWriter("cipherMessage.txt");
        System.out.println(newMessage + "\n");
        outM.println(newMessage);
        outM.close();
    }
   
    public static void createNewAlphabet()
    {
        int number;
        while(alphabet.size() > newAlphabet.size())
        {
            number = (int)(Math.random() * 26);
            if(newAlphabet.contains(alphabet.get(number)))
            {    
                newAlphabet.remove(alphabet.get(number));
            }
            else 
            {
                newAlphabet.add(alphabet.get(number));
            }
        }
    }
   
    public static void displayNewAlphabet(ArrayList<Character> newAlphabet)
    {
        for (int loop = 0; loop < 26; loop++)
        {
            System.out.print(newAlphabet.get(loop));
        }
    }
  
    public static String encrypt(String message)
    {
        String upper = message.toUpperCase();
        String newMessage = "";
        char newLetter;
        char letter;
        int letterPosition;
        //Runs through the message and encrypts it
        for (int loop = 0; loop < message.length(); loop++)
        {
            if(upper.charAt(loop) >= 65 && upper.charAt(loop) <= 90)
            {
                letterPosition = alphabet.indexOf(upper.charAt(loop));
                newMessage += newAlphabet.get(letterPosition);
            }
            else
            {
                newMessage += upper.charAt(loop);
            }
        }     
        return newMessage;
    }
}



/**
 * This class is used to decrypt messages with a Caesar shift number.
 * 
 * @author Christopher Goad
 * @version 3-13-12
 */

import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.IOException;
import java.io.PrintWriter;
public class ChrisGoadDecipher
{
    private static ArrayList<Character> cipherAlphabet = new ArrayList<Character>();
    private static ArrayList<Character> alphabet = ChrisGoadCipherTester.alphabet;
    
    public ChrisGoadDecipher()
    {   
        
    }
   
    public static void getInput() throws IOException
    {
        createcipherAlphabet();
        System.out.println("Here is the alphabet being used for decryption:");
        displayNewAlphabet(cipherAlphabet);
        String message = message();
        String newMessage = decrypt(message);
        System.out.println("Here is the decrypted message using the cipher alphabet: ");
        System.out.println(newMessage + "\n");
    }
    
    public static String message() throws IOException
    {
        File file = new File("cipherMessage.txt");
        Scanner in = new Scanner(file);
        String message = "";
        while(in.hasNext())
        {
            message = in.nextLine();
        }
        return message;
    }
    
    public static void displayNewAlphabet(ArrayList<Character> newAlphabet)
    {
        for (int loop = 0; loop < 26; loop++)
        {
            System.out.print(cipherAlphabet.get(loop));
        }
    }
    
    public static void createcipherAlphabet() throws IOException
    {
        File file = new File("cipherAlphabet.txt");
        Scanner in = new Scanner(file);
        while(in.hasNext())
        {
           String character = in.next();
           if(character == ","){}
           else if(character == "["){}
           else if(character == "]"){}
           else 
           {
               String alphabet = "abcdefghijklmnopqrstuvwxyz";
               int num = alphabet.indexOf(character);
               String letter = alphabet.substring(num, num+1);
               for (char c : letter.toCharArray()) 
               {
                   cipherAlphabet.add(c);
                }
           }
        }
        
    }
    
    public static String decrypt(String message)
    {
        String upper = message.toUpperCase();
        String newMessage = "";
        char newLetter;
        char letter;
        int letterPosition;
        //Runs through the message and encrypts it
        for (int loop = 0; loop < message.length(); loop++)
        {
            if(upper.charAt(loop) >= 65 && upper.charAt(loop) <= 90)
            {
                letterPosition = alphabet.indexOf(upper.charAt(loop));
                newMessage += cipherAlphabet.get(letterPosition);
            }
            else
            {
                newMessage += upper.charAt(loop);
            }
        }     
        return newMessage;
    }
}

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Cipher and Decipher

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Cipher and Decipher

Posted 23 March 2013 - 02:23 PM

Post the error, copied and pasted from exactly as it appears at your end, in code tags if it's long. (Good first post, by the way.)
Was This Post Helpful? 0
  • +
  • -

#3 chrisgoad3882  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-March 13

Re: Cipher and Decipher

Posted 23 March 2013 - 02:48 PM

Okay, when I run my tester class I get the following error

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Cipher and Decipher

Posted 23 March 2013 - 02:52 PM

Exactly as it appears, including the entire stack trace. Don't post just what you think is important. Please post all of it.
Was This Post Helpful? 0
  • +
  • -

#5 chrisgoad3882  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-March 13

Re: Cipher and Decipher

Posted 23 March 2013 - 02:58 PM

I'm sorry but I'm not understanding what you are talking about. Can you please just run my code and see for yourself?
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Cipher and Decipher

Posted 23 March 2013 - 03:06 PM

This is my sample run:
===============================
     [A] Chris Goad Cipher 
     [B] Chris Goad Decipher 
           [Q] Quit           
===============================
Please select an letter option from the above menu and hit enter: a
Here is the alphabet being used for encryption:
KAPZTBEOGCLQWSFUVIJNXDRMYH
Enter the phrase that will be encrypted: this is my phrase
Here is the encrypted message using the cipher alphabet: 
NOGJ GJ WY UOIKJT

There doesn't appear to be anything wrong, problem solved!

Perhaps you could post a similar sample run that shows the problems you'd like help with, copied and pasted from your own terminal, including any errors that you get, to show us the details we need to help you.
Was This Post Helpful? 0
  • +
  • -

#7 chrisgoad3882  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-March 13

Re: Cipher and Decipher

Posted 23 March 2013 - 03:11 PM

I'm sorry but I forget to put that the problem is in the decryption class. Use option B.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,518
  • Joined: 20-September 08

Re: Cipher and Decipher

Posted 23 March 2013 - 05:28 PM

http://technojeeves....-cipher-in-java
Was This Post Helpful? 0
  • +
  • -

#9 chrisgoad3882  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-March 13

Re: Cipher and Decipher

Posted 23 March 2013 - 05:44 PM

Thanks for the reply. But I understand how to Caesar shift in java. I am having trouble reading the alphabet in from the text file and assigning into an arraylist
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1