0

I am almost finished with a program that takes user input and encrypts the message then displays it back. For some reason I am unable to pass the string to my encryption method and return it. Does anyone see where I have gone wrong?

Thank you so much to anyone who responds!

public static String doEncryption(String s)
{
    char alphabet[]  = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
        'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
        'w', 'x', 'y', 'z' };
    char key[] = { 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
        'x', 'y', 'z', 'a' };

    char encoded[] = new char[(s.length())];
    String encrypted="";
    int j=0;
    for(int i=0; i<s.length(); i++){
        //System.out.println(s.charAt(i));
        boolean isFound = false;
        j = 0;
        while (j < alphabet.length && !isFound){


            if (alphabet[j]==s.charAt(i)){
               encrypted=encrypted+key[j];
               isFound=true;
            }
            j++; 
        }

        if(j>=alphabet.length){
            encrypted=encrypted+s.charAt(i);
        }
       }        
    return (new String(encrypted));
}


public static void main(String args[])
{
    String match = "QUIT";
    String en = "";
    System.out.println("Welcome to the Encoding Program!");
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the text you want encoded below.  Type QUIT when finished. ");
    en = sc.nextLine();
    String trim = en.substring(0, en.indexOf("QUIT"));
    doEncryption(trim.toLowerCase());
    //String en = doEncryption(sc.nextLine().toLowerCase());
    System.out.println("The text is encoded as: " + trim.toUpperCase());
    sc.close();
}

}

3
  • 3
    For some reason ? what are those Commented Mar 12, 2015 at 19:32
  • 1
    What do you mean by you can't? do you get an error? does nothing happen? Be more specific please Commented Mar 12, 2015 at 19:33
  • 1
    You have to assign the result to some other variable. Commented Mar 12, 2015 at 19:34

2 Answers 2

2

Your method returns the updated string, so you'll want to use that return value when calling it. Change:

doEncryption(trim.toLowerCase());

to

String updatedValue = doEncryption(trim.toLowerCase());

or reuse trim if you like:

trim = doEncryption(trim.toLowerCase());

...and then use updatedValue or trim to show the result.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah that did it thanks! Now that its all working, I am running test cases and my first one presents an error! If I type in say "ABCDEFGHIJKLMNOPQRSTUVWXYZ" where I would expect encrpytion to create "BCDEFGH....." I get an error: "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1954) at cipher2.main(cipher2.java:46) What is causing this? Thanks!
@still2blue See my answer, I prevent you about this. It happen because you have entered a string which does not contains "QUIT" so index-of return -1 which is not a valid bound for substring.
2

Simply because you never re-assign the value.

Change

doEncryption(trim.toLowerCase());

to

trim = doEncryption(trim.toLowerCase());

then System.out.println("The text is encoded as: " + trim.toUpperCase()); will display the correct value.

Beware of this line

en.substring(0, en.indexOf("QUIT"));

It will throw a java.lang.StringIndexOutOfBoundsException if "QUIT" is not in the string.

I think you want the program to execute until "QUIT" is entered so you need to loop over until the input is "QUIT". Your main method would look like this

String match = "QUIT";
String en = "";
System.out.println("Welcome to the Encoding Program!");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the text you want encoded below.  Type QUIT when finished. ");
do
{
    en = sc.nextLine();

    if(!en.toUpperCase().equals(match))
    {
        en = doEncryption(en.toLowerCase());

        System.out.println("The text is encoded as: " + en.toUpperCase());
    }
} while(!match.equals(en.toUpperCase()));
sc.close();

When you want to exit, simply input quit.

5 Comments

So then what do I use to remove QUIT? I made the change you suggested but still get the error because of the substring line..
I figured it out, I was forgetting QUIT, now though if I type "ABCDEFGHIJKLMNOPQRSTUVWXYZQUIT" it doesn't recognize QUIT and encrypts it, how do I change that?
validate for if(string.contains("QUIT"))
It's all working fine except for one thing: Welcome to the Encoding Program! Enter the text you want encoded below. Type QUIT when finished. ABCDEFGHIJKLMNOPQRSTUVWXYZ quit QUIT The text is encoded as: BCDEFGHIJKLMNOPQRSTUVWXYZAZ RVJU why is it adding an extra Z at the end of the encrypted message?
Got it! It was a condition in my loop, had <= instead of <. That fixes the Z but then if I use that, no spaces are included in my output! Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.