Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with my caesar cipher code

    This version of Ceaser Cipher code need help fixing it it doesnt print more than one word

    PHP Code:
    import java.util.Scanner;
    public class 
    CaesarCipher
    {
      public static 
    void main(String[] args)
      {
          
    Scanner scan=new Scanner(System.in);
          
    String word null;
          
          
    System.out.println("Please input the word or phrase that you want to encode: ");
          
    String input scan.nextLine();
          
          
    input=input.toLowerCase();
          
          
    System.out.println("Please input the encryption key that you want to use: ");
          
    int ekey scan.nextInt();
          
          
    String output="";
          
    String alpha="abcdefghijklmnopqrstuvxyz";
          
    //            0123456789
          
    int index 0;
          while(
    input.length() > 0)
          {
             
    int space input.indexOf(" ");
             
             if(
    space == -1)
              {
                
    word input;
                
    input "";
              }
              else
                
    word input.substring(0,space);
             
           while (
    index<word.length())
            {
              
    int charposition=alpha.indexOf(word.substring(index,index+1));
              if (
    charposition==-1)
              {
                  
    output=output+word.substring(index,index+1);
              }
              else
              {
                  if (
    ekey>26)
                  {
                   
    ekey=(ekey%26);
                  }
                  else
                 {
                  
    ekey=ekey;
                  }
              }
              
              
    output=output+(alpha.substring(charposition+ekey,charposition+ekey+1));
              
              
    index++;
            }
            
           
    input input.substring(space+1);
           
    index++;
        }
          
    System.out.println(output);
      }


    If you couldn't find out how to fix the first one this is the second version where it prints everything though if you have a space or a symbol it adds the letter "a" after the symbol or space.


    PHP Code:
    import java.util.Scanner;
    public class 
    CaesarCipher
    {
      public static 
    void main(String[] args)
      {
          
    Scanner scan=new Scanner(System.in);
          
          
          
    System.out.println("Please input the word or phrase that you want to encode: ");
          
    String input scan.nextLine();
          
          
    input=input.toLowerCase();
          
          
    System.out.println("Please input the encryption key that you want to use: ");
          
    int ekey scan.nextInt();
          
          
    String output="";
          
    String alpha="abcdefghijklmnopqrstuvxyz";
          
    //            0123456789
          
    int index 0;
        
             
           while (
    index<input.length())
            {
              
    int charposition=alpha.indexOf(input.substring(index,index+1));
              if (
    charposition==-1)
              {
                  
    output=output+input.substring(index,index+1);
              }
              else
              {
                  if (
    ekey>26)
                  {
                   
    ekey=(ekey%26);
                  }
                  else
                 {
                  
    ekey=ekey;
                  }
              }
              
              
    output=output+(alpha.substring(charposition+ekey,charposition+ekey+1));
              
              
    index++;
            }
            
         
          
    System.out.println(output);
      }

    Last edited by VIPStephan; 11-11-2016 at 08:37 PM. Reason: added code BB tags

  2. #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    TX
    Posts
    3,444
    Thanks
    55
    Thanked 542 Times in 536 Posts
    Wrong forum.

    That's Java.
    This is Javascript

    Ask the moderators to move it to get a better response.

  3. #3
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    10,480
    Thanks
    6
    Thanked 1,230 Times in 1,200 Posts
    Moved from the JavaScript forum to the Java forum.

  4. #4
    New to the CF scene
    Join Date
    Nov 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to set up a cycle.


 

Tags for this Thread

Posting Permissions

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