HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Rot -n algorithm in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
algorithmrotjava

Problem

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 = 65 && ch = 97 && ch  0) {
        File fFile = new File(args[0]); //Input file having the first line as the  key 
        Fightsopa fsopa = new Fightsopa();
        fsopa.handler(fFile);
    }
}
}

Solution

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 map = new HashMap();
    for (int i = 0; i < len; i++) {
        map.put(abcABC.charAt(i), abcABC.charAt((i + n + len) % len));
    }

    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < 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.

Code Snippets

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

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

    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < 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();
}

Context

StackExchange Code Review Q#7241, answer score: 11

Revisions (0)

No revisions yet.