Click here to Skip to main content
Rate this: bad
good
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 at 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 at 20-Feb-13 14:06pm
   
Gets my 5.
ProgramFOX at 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 at 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
0 Maciej Los 645
1 OriginalGriff 453
2 CPallini 239
3 CHill60 180
4 Peter Leow 175
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,898
2 Maciej Los 3,159
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
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