• Log in or sign up for an account here: https://www.sythe.org/login

Any way to make my program simpler?

Snow Fury

Active Member

$5 USD Donor New
Joined
Sep 10, 2011
Sythe Gold
0
Messages
165
Location
Somewhere in the Pacific
Hey I've been working on an encryption program just for fun and I need to know a way to make my code easier to read or if there is a simpler way to do this. I tried using a switch but I couldn't get that to work and I don't want to do a big line of if statements for the whole entire alphabet.

Here's my code:
package encryption;
import java.io.*;
import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please input a sentence to encrypt: ");
String input = in.nextLine();

Encryption myCrypt = new Encryption();
String encryptdInput = myCrypt.encrypt(input.toLowerCase());
System.out.println(encryptdInput);

System.out.print("Please input a sentence to decrypt: ");
String de = in.nextLine();
String decryptdInput = myCrypt.decrypt(de);
System.out.println(decryptdInput);


}

}
class Encryption{

public String encrypt(String s){
for (int i = 0; i<s.length(); i++){

if (s.contains("a")){
s = s.replace("a", "%3");

} else if (s.contains("b")){
s = s.replace("b", "!83&~");

} else if (s.contains("c")){
s = s.replace("c", ".2");

} else if (s.contains("d")){
s = s.replace("d", "!83&`");
}
}return s;

}
public String decrypt(String s){
for (int i = 0; i<s.length(); i++){

if (s.contains("%3")){
s = s.replace("%3", "a");

} else if (s.contains("!83&~")){
s = s.replace("!83&~", "b");

} else if (s.contains(".2")){
s = s.replace(".2", "c");

} else if (s.contains("!83&`")){
s = s.replace("!83&`", "d");

}
} return s;

}

}
 
You could change all of the replace() function calls to replaceAll() so that it replaces all occurrences of that character. This would remove the need of the for loops iterating over the string length. However, it would still be wildly inefficient because replaceAll() is a regular expression search and thus inherently slower than just a plain string search.

Since you did not make all of your encrypted versions the same number of characters as each other it is quite complex to make the decryption any faster than just using replaceAll() method calls.

Encryption can be made efficient because every input is the same length (one character). Your best bet is probably be iterating over the string one character at a time and looking up its encrypted version in an array. Then, adding the corresponding encrypted character(s) to an output string (using StringBuilder for even more efficiency).

Here's some code on how to do encryption. I did not compile this example but it should give you an idea, feel free to ask more questions. This only works for lowercase but it can be adapted to handle other types of characters with just a couple extra if statements.
Code:
// Lookup table for encrypting characters
String[] encrypted = new String[] { "%3", "!83&~",  ".2", "!83&`" }
StringBuilder output = new StringBuilder();
for(i=0; i<s.length(); i++){
   // If the table has an encrypted version of this character add it to output
   if(s.charAt(i) - 'a' < encrypted.length) {
      output.append(encrypted[s.charAt(i)-'a']);
   }
}
return output.toString();
 
I actually do have a few more questions I'm trying to prepare for my upcoming test for my Comp Sci course and my text books I downloaded aren't helping...

These are some of the things I need to know:
Construct switch blocks:
Switch(){
case 1:
System.out.println("herpaderp");
break;
default:
System.out.println("derp");
}
Is that a switch block?

Explain why non-static variables cannot be referenced from a static context:
I don't know the differences between static variables and non-static, could you explain please?






Yea that's all that I need help on really and incase you wanted to know what else is on the test is here:
Declare and initialize variables and constants using appropriate names and data types
Print output to the console
Conduct basic string operations, including concatenating string literals and string variables (you will not be required to remember particular methods of the String class, such as subString() and toUpperCase())
Use numeric data types correctly, understanding integer division
Correctly predict the effect of the — decrement and ++ increment operators
Use the modulo % operator
Write code to read user input from the console
Use brackets in expressions
Use the assignment (=) and comparison (==) operators correctly
Use the logical operators and (&&), or (||) and not (!) correctly
Construct if blocks, using else if and else, and nested if blocks as necessary
Construct switch blocks
Use for, while and do… while loops (you will be expected to write them from scratch)
Create classes, including constructors, methods and instance variables
Declare and instantiate object types
Compare strings and other object types
Use the keywords private and public correctly
Declare and initialize arrays of primitives and objects
Loop through each element of an array
Understand and implement parallel arrays
Predict when NullPointerExceptions will occur
Understand and use correctly the keyword static and the understand the difference between class variables and instance variables
Explain why non-static variables cannot be referenced from a static context
 
I added you on MSN. Can answer more on there.
 
Use code blocks not quote blocks to preserve indentation.
Code:
for name in names:
   print name
 
Runescape Gamblingrunescape gambling
Back
Top