2 Replies - 2016 Views - Last Post: 15 March 2012 - 07:12 AM Rate Topic: -----

#1 hihoboy4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-March 12

I am working on the Beaufort Cipher to decode some of his work

Posted 14 March 2012 - 08:20 PM

So I have a lot of code in here. Everything compiles but when i run my the program it returns a null at the end. Thanks.
import java.util.*;
public class letter
{
    char[] letters = {'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'};
    int temp;
    public letter()
    {

    }
    public char getLetter(int y)
    {
        char z = letters[y];
        return z;
    }
    public int getValue(char x)
    {
        
        for(int y = 0; y<= letters.length; y++)
        {
            if( x == letters[y])
            {
                temp = y;
            }
        }
        return temp;
    }
}


import java.util.*;
public class Cipher
{
    String text,key1,key2,key3,text2;
    int way,num1,num2,num3,k,t,c;
    public Cipher(String x, String y)
    {
        key1 = y;
        text = x;
    }
    public void same()
    {
        int num=text.length();
        int num2=key1.length();
        if(num1==num2)
        {
            key2=key1;
        }
        else if(num1 > num2)
        {
            num3=num2;
            while(num1>num3)
            {
                for(int b =0; b<num2; b++)
                {
                    String d;
                    d = key1.valueOf((key1.charAt(B)/>));
                    key3.concat(d);
                    num3++;
                }
            }
            key2=key3;
        }
        else
        {
            key2=(key1.substring(0,num1));
        }
    }
    public void crypt()
    {
        letter Letter = new letter();
        letter Number = new letter();
        for(int g=0;g<num1; g++)
        {
            k = Letter.getValue(key2.charAt(g));
            t = Letter.getValue(text.charAt(g));
            if(k>t)
            {
                c = (k-t+1);
            }
            else
            {
                c = ((k+26)-t+1);
            }
            char f; 
            f = Number.getLetter(c);
            text2=text;
            text2.replace(key2.charAt(g),f);
        }
    }
    public String getCrypt()
    {
        return text2;
    }

}


import java.util.*;
public class BeaufortCipher
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        String cryptic, key, plain, text;
        System.out.println("Welcome to the Beaufort Cipher");
        System.out.println("To decrypt please enter 1. To encrypt please enter 2.");
        int x = scan.nextInt();
        if(x==1)
        {
            System.out.println("What is the message that you would like to decrypt? Please no spaces");
            cryptic = scan.next();
            cryptic.toUpperCase();
            System.out.println("What is the Key for this message? Please no spaces");
            key = scan.next();
            key.toUpperCase();
            Cipher plainT = new Cipher(cryptic,key);
            text= plainT.getCrypt();
            System.out.println("Your decrypted message is: " + text);
        }
        else if(x==2)
        {
            System.out.println("What is the message that you would like to Encrypt? Please no spaces");
            plain = scan.next();
            plain.toUpperCase();
            System.out.println("What is the Key you would like to use for this message? Please no spaces");
            key = scan.next();
            key.toUpperCase();
            Cipher crypticT = new Cipher(plain,key);
            text = crypticT.getCrypt();
            System.out.println("Your Encrypted message is: " + text);
        }
        else
        {
            System.out.println("That was not the number 1 or 2. Goodbye");
    }
}
}
 }


This post has been edited by pbl: 14 March 2012 - 08:52 PM
Reason for edit:: Code tags added. Please :code:


Is This A Good Question/Topic? 0
  • +

Replies To: I am working on the Beaufort Cipher to decode some of his work

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: I am working on the Beaufort Cipher to decode some of his work

Posted 15 March 2012 - 06:45 AM

hihoboy4: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # 2. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Feb 2012
Spoiler



Was This Post Helpful? 1
  • +
  • -

#3 wrightm96   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 06-March 12

Re: I am working on the Beaufort Cipher to decode some of his work

Posted 15 March 2012 - 07:12 AM

You never actually call your same() and crypt() methods. You made a mistake using "num" instead of "num1" in Cipher. Also, in the same spot you did this, you mistakenly recreated variables called "num1" and "num2" instead of using your global variables.

Also take a look at your algorithm. I believe you made a mistake when doing it, as the program results in an ArrayIndexOutOfBounds error after this stuff has been fixed.

As tlhIn`toq said, the debugger helps a ton. Using print statements to debug if also a good method, but learning how to use a debugger will help a lot more than print statements.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1