Rot -n algorithm in Java This is a rot-n (rotate n times) encrypt/decrypt algorithm, where n can be any integer (+/-).

It's written to encrypt/decrypt all the alphabets (upper/lower case) and leaving behind all non-alphas.

rot-n where n is the key, is given in the first line of input file.

Everything is working fine.  I just need help in optimizing it.


  Challenge description
  
  Your submission will be tested against an input file that contains
  ASCII characters. The input file starts at the very first line with a
  N-digits positive or negative integer number that is your cipher,
  followed by a new-line character. The second line starts with the
  payload of the encrypted text until the end of file. Note that only
  words (alphanumeric sequences) are encrypted and that every other
  character (i.e. punctuation) must not be processed by your algorithm
  and must also be copied ‘as is’ to the standard output.*


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Fightsopa {

public void process(String input, int n) {
    int abcd = n;
    if (abcd &lt; 0) {
        abcd = (26 - ((0 - abcd) % 26));
    }
    int rot = 26 - abcd % 26;  //for decrypting
 // int rot=       abcd % 26;  //for encrypting
    char out = 0;
    for (char ch : input.toCharArray()) {
        if (ch &gt;= 65 &amp;&amp; ch &lt;= 90) {
            if (ch + rot &lt;= 90) {
                out = (char) (ch + rot);
                System.out.print(out);
            } else {
                out = (char) (ch + rot - 26);
                System.out.print(out);
            }

        } else if (ch &gt;= 97 &amp;&amp; ch &lt;= 122) {
            if (ch + rot &lt;= 122) {
                out = (char) (ch + rot);
                System.out.print(out);
            } else {
                out = (char) (ch + rot - 26);
                System.out.print(out);
            }
        } else {
            System.out.print(ch);
        }
    }
}

public void handler(File fFile) throws FileNotFoundException {
    Scanner scanner = new Scanner(new FileReader(fFile));
    int rot = 0;
    int count = 0;
    while (scanner.hasNextLine()) {
        if (count == 0) {
            rot = scanner.nextInt();
            scanner.nextLine();
        } else {
            String wordp = scanner.nextLine();
            process(wordp, rot);
            System.out.println();
        }
        count++;
    }
    scanner.close();

}

public static void main(String args[]) throws FileNotFoundException {
    if (args.length &gt; 0) {
        File fFile = new File(args[0]); //Input file having the first line as the  key 
        Fightsopa fsopa = new Fightsopa();
        fsopa.handler(fFile);
    }
}
}
First, your process method violates the Single Responsibility Principle: It calculates something, and it prints it out. Such coupling makes it inflexible. It's better to return a String, and letting the caller decide what to do with it.

Second, you're code is way too complicated, handling all the different cases which are very dependent on the ASCII layout. Imagine next you want to support digits as well - oh my...

I would suggest to do something along the lines...

public String process(String input, int n) {
    String abc = "abcdefghijklmnopqrstuvwxyz";
    String abcABC = abc + abc.toUpperCase();
    int len = abcABC.length();

    Map&lt;Character, Character&gt; map = new HashMap&lt;Character, Character&gt;();
    for (int i = 0; i &lt; len; i++) {
        map.put(abcABC.charAt(i), abcABC.charAt((i + n + len) % len));
    }

    StringBuilder sb = new StringBuilder();
    for(int i = 0; i &lt; input.length(); i++) {
        Character ch = map.get(input.charAt(i));
        if (ch == null) {
            throw new IllegalArgumentException("Illegal character " + input.charAt(i));
        }
        sb.append(ch);
    }
    return sb.toString();
}


Note that I need to know nothing about ASCII encoding using that approach, so supporting other chars is a no-brainer.