Guest User

Untitled

a guest
Jun 25th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. public class CaesarCipher {
  2.     public static void main(String[] strings) {
  3.         int offset = 0;
  4.         char c;
  5.         int shift= 0;;
  6.         boolean isComplete = false;
  7.        
  8.         while (offset<=-25 || offset == 0  || offset >=25 || offset!=-999 || offset!=999) {
  9.             TextIO.putln("Please enter the shift value (between -25..-1 and 1..25)");
  10.             offset = TextIO.getlnInt();
  11.             if (offset<=-25 || offset>=25 || offset == 0) {
  12.                 TextIO.putln(offset+" is not a valid shift value.");
  13.             };
  14.         }
  15.  
  16.         TextIO.putln("Using shift value of "+offset);
  17.        
  18.         while (isComplete == false) {
  19.             TextIO.putln("Please enter the source text (empty line to quit)");
  20.             String m= TextIO.getln();
  21.            
  22.             if (m.equals("")) {
  23.                 TextIO.putln("Bye.");
  24.                 isComplete =true;
  25.                 return;
  26.             }
  27.    
  28.             TextIO.putln("Source   :"+m);
  29.             m = m.toUpperCase();
  30.             String result ="";
  31.                        
  32.             if (offset == 999) {
  33.                 boolean posishift = true;
  34.                 offset = 1;
  35.             }
  36.            
  37.             if (offset == -999) {
  38.                 boolean negashift = true;
  39.                 offset = -1;
  40.             }
  41.            
  42.             for (int i=0; i<=m.length() -1; i++ )   {
  43.                 c = m.charAt(i);
  44.                
  45.                 if (  (int)c >=65 && (int)c <=90)   {
  46.                     int letter = c - 'A';
  47.                     shift = (26 + letter + offset) % 26;
  48.                     char newletter = (char) (shift + 'A');
  49.                     result += newletter;
  50.                 }
  51.                 if (posishift) offset ++;
  52.                 if (negashift) offset --; else result += c;
  53.             }
  54.        
  55.             TextIO.putln("Processed:"+result);
  56.        
  57.         }
  58.     }
  59.        
  60. }
Add Comment
Please, Sign In to add comment