Results 1 to 9 of 9

Thread: Enciphing

  1. #1
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default Enciphing

    Can someone please help me fix this. It compiles correctly but it outputs the wrong things. I'm not sure what I've done wrong.
    Java Code:
    import java.util.Scanner;
    public class Cipher
    {
       public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
            String Input;
            String Output = "";
            System.out.println("Input Word");
            String Encipher = keyboard.nextLine();
            Encipher.replaceAll("\\s","");
              
            Integer EncipherPosition = 0;
            String Cipher = "";
            for (EncipherPosition = 0; EncipherPosition < Encipher.length(); EncipherPosition ++)
                {
                    char EncipherChar = Encipher.charAt(EncipherPosition);
                    String EncipherString = "" + EncipherChar;
                    boolean temp = Cipher.contains(EncipherString);
                    if (temp == false)
                        {
                            Cipher += Encipher.charAt(EncipherPosition);
                        }  
                }
            System.out.println("This is what you Input: " + Cipher);
             
            String alphabet = "abcdefghijklmnopqrstuvwxyz";
            Integer alphabetPosition = 0;
            for (alphabetPosition = 0;alphabetPosition < alphabet.length();alphabetPosition ++)
                {
                    char alphabetTempChar = alphabet.charAt(alphabetPosition);
                    String alphabetTempString = "" + alphabetTempChar;
                    boolean alphabetTemp = Cipher.contains(alphabetTempString);
                    if (alphabetTemp == false)
                        {
                            Cipher += alphabet.charAt(EncipherPosition);
                        }
                }
              
            System.out.println("This is your ciphered text" + Cipher + alphabet + Cipher);
            System.out.println("Input message:");
            Input = keyboard.nextLine();
              
            Integer position = 0;
            for (position = 0; position < alphabet.length(); position ++)
                {
                    char CipherChar = Cipher.charAt(position);
                    String CipherString = "" + CipherChar;
                    char alphabetChar = alphabet.charAt(position);
                    String alphabetString = "" + alphabetChar;
                    Input.replace(alphabetString,CipherString);
                }
            System.out.println("This is your encoded message : " + Input);
            }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Enciphing

    it outputs the wrong things
    You need to be more specific. What output do you expect, and what wrong output do you get?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default Re: Enciphing

    This is what happens when I run the program
    Input Word
    testing
    This is what you Input: tesing
    This is your ciphered texttesinghhhhhhhhhhhhhhhhhhhabcdefghijklmnopqrstu vwxyztesinghhhhhhhhhhhhhhhhhhh
    Input message:
    test one
    It outputs pretty near random letters instead of an enciphered one and doesn't finish the program because it should also output an enciphered message that was input.
    I get these errors:
    Java Code:
    java.lang.StringIndexOutOfBoundsException: String index out of range: 25
    	at java.lang.String.charAt(String.java:686)
    	at Cipher.main(Cipher.java:47)
    Thanks for your help!

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Enciphing

    Java Code:
    Cipher += alphabet.charAt(EncipherPosition);
    Using too much copy paste can be a problem.

  5. #5
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default Re: Enciphing

    What's wrong with it?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Enciphing

    Can you post what the output from the program should be?

    You get the StringIndexOutOfBoundsException: String index out of range: 25
    because the index you are using is past the end of the String. Check your logic to make sure the index stays within the length of the String.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Enciphing

    Quote Originally Posted by DuncanHollows View Post
    What's wrong with it?
    Check to see where EncipherPosition is last defined. You copied the code fragment above it and forgot to change the variable name.

  8. #8
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default Re: Enciphing

    Quote Originally Posted by Norm View Post
    Can you post what the output from the program should be?

    You get the StringIndexOutOfBoundsException: String index out of range: 25
    because the index you are using is past the end of the String. Check your logic to make sure the index stays within the length of the String.
    Quote Originally Posted by Diargg View Post
    Check to see where EncipherPosition is last defined. You copied the code fragment above it and forgot to change the variable name.
    I'm not sure what either of you mean

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Enciphing

    What should be printed out by the program?
    Your post#3 shows some of what is printed out, what should the rest of the print out be?

    What is the length of the String where the exception occurs? What is the value of the index when the exception occurs?
    Add a println statement just before that statement and print out those values.
    Last edited by Norm; 04-21-2012 at 01:20 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Enciphing Text based on a word
    By DuncanHollows in forum New To Java
    Replies: 1
    Last Post: 03-27-2012, 08:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •