1 Replies - 144 Views - Last Post: 23 March 2013 - 05:10 PM Rate Topic: -----

#1 chrisgoad3882  Icon User is offline

  • New D.I.C Head

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

java.lang.StringIndexOutOfBoundsException

Posted 23 March 2013 - 05:09 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. Also the error occurs when you use option B which is the Decryption class. 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 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;
    }
}





/**
* 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;
    }
}



Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: java.lang.StringIndexOutOfBoundsException

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: java.lang.StringIndexOutOfBoundsException

Posted 23 March 2013 - 05:10 PM

Don't post duplicate threads.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1