Click here to Skip to main content
12,348,946 members (50,679 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java
this code of caesar encryption

have some error ,, pleas help me


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 20-Feb-13 5:35am
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,

In the line
chyfer + = STR.charAt((kay+y)%26); 
there's a space between + and =. Remove the space:
chyfer += STR.charAt((kay+y)%26); 
Hope this helps.
  Permalink  
Comments
Marcus Kramer 20-Feb-13 14:06pm
   
Gets my 5.
ProgramFOX 20-Feb-13 14:21pm
   
Thank you!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

After modified the mistakes got the following code, but it is not run >>


What is the reason ؟؟

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 ;
}}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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)
  Permalink  
Comments
ProgramFOX 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100