Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this code of caesar encryption

have some error ,, pleas help me


Java
package encryption;
import java.util.*;
/**
 *
 * @author Toshiba
 */
public class Encryption {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      //  ENCR n =new ENCR();
       Scanner S = new Scanner(System.in);
       System.out.print(" Enter the text");    
        String massege  = S.next();
       System.out.print(" Enter the kay");    
        int key = S.nextInt();
    }
  
    public String NN (  String massege  , int kay )
    
    {
    
    String  STR= "ABCDEFGHIJKLMNOPQRSTWXYZ" ; 
    char current;
    String chyfer= ""; 
    for (int i=0; i<=massege .length();i++)
    {
    
    current = massege .charAt(i);
    
    for (int y= 0 ; y<=STR.length(); y++) {
            if(current==STR.charAt(y))
            {
             
             chyfer + = STR.charAt((kay+y)%26);
             
             return chyfer ;
             
             }
        }
    }
     return chyfer ;
    }
}
Posted
Comments
joshrduncan2012 20-Feb-13 12:02pm    
Please tell us what error(s) you are getting and where they are pointing to. No one here will go through the code for you to see the errors for themselves.

Hi,

In the line
Java
chyfer + = STR.charAt((kay+y)%26); 

there's a space between + and =. Remove the space:
Java
chyfer += STR.charAt((kay+y)%26); 

Hope this helps.
 
Share this answer
 
Comments
fjdiewornncalwe 20-Feb-13 14:06pm    
Gets my 5.
Thomas Daniels 20-Feb-13 14:21pm    
Thank you!
After modified the mistakes got the following code, but it is not run >>


What is the reason ؟؟

C#
package encryption;
import java.util.*;
public class Encryption {
 public static void main(String[] args) {
        Encryption enc = new Encryption();
       Scanner S = new Scanner(System.in);
       System.out.println(" Enter the text");
        String massege  = S.next();
       System.out.println(" Enter the kay");
        int key = S.nextInt();

        System.out.println( " the word chyfer is " + enc.NN( massege, key) );
    }
    public  String NN (  String massege  , int kay )
    {
    String  STR= "ABCDEFGHIJKLMNOPQRSTWXYZ" ;
    char current;
    String chyfer= "";
    for (int i=0; i<=massege .length();i++)
    {
    current = massege .charAt(i);
    for (int y= 0 ; y<=STR.length(); y++) {
            if(current==STR.charAt(y))
            {
             chyfer+=STR.charAt((kay+y)%26);}
    }}
     return chyfer ;
}}
 
Share this answer
 
thi is the output

Enter the text
jg
Enter the kay
2
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 24
at java.lang.String.charAt(String.java:686)
at encryption.Encryption$Encryp.NN(Encryption.java:28)
at encryption.Encryption.main(Encryption.java:13)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
 
Share this answer
 
Comments
Thomas Daniels 21-Feb-13 11:32am    
Try i < massege.length() instead of i<=massege .length()
Try also y< STR.length() instead of y<=STR.length()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900