Hi, I am having trouble with a program in which I am promted to encrypt a string by using an array with the normal alphabet compared to an array with a randomly ordered alphabet. I am having trouble with the conversion. Can anyone see where I am going wrong?
Here is the class with the arrays:
Here is my client application:
Thank you ahead of time for any helpful replies.
The problematic area is likely to be on the client section starting on line 19 onwards.
Here is the class with the arrays:
import java.util.*;
public class Final
{
private char [] alphabet = new char [26];
private char [] encrypt = new char [26];
private String sentence;
public Final()
{
alphabet[0] = 'a';
alphabet[1] = 'b';
alphabet[2] = 'c';
alphabet[3] = 'd';
alphabet[4] = 'e';
alphabet[5] = 'f';
alphabet[6] = 'g';
alphabet[7] = 'h';
alphabet[8] = 'i';
alphabet[9] = 'j';
alphabet[10] = 'k';
alphabet[11] = 'l';
alphabet[12] = 'm';
alphabet[13] = 'n';
alphabet[14] = 'o';
alphabet[15] = 'p';
alphabet[16] = 'q';
alphabet[17] = 'r';
alphabet[18] = 's';
alphabet[19] = 't';
alphabet[20] = 'u';
alphabet[21] = 'v';
alphabet[22] = 'w';
alphabet[23] = 'x';
alphabet[24] = 'y';
alphabet[25] = 'z';
final String randombet = "qwertyuioplkjhgfdsazxcvbnm";
final int N = randombet.length();
Random r = new Random();
encrypt[0] = randombet.charAt(r.nextInt(N));
encrypt[1] = randombet.charAt(r.nextInt(N));
encrypt[2] = randombet.charAt(r.nextInt(N));
encrypt[3] = randombet.charAt(r.nextInt(N));
encrypt[4] = randombet.charAt(r.nextInt(N));
encrypt[5] = randombet.charAt(r.nextInt(N));
encrypt[6] = randombet.charAt(r.nextInt(N));
encrypt[7] = randombet.charAt(r.nextInt(N));
encrypt[8] = randombet.charAt(r.nextInt(N));
encrypt[9] = randombet.charAt(r.nextInt(N));
encrypt[10] = randombet.charAt(r.nextInt(N));
encrypt[11] = randombet.charAt(r.nextInt(N));
encrypt[12] = randombet.charAt(r.nextInt(N));
encrypt[13] = randombet.charAt(r.nextInt(N));
encrypt[14] = randombet.charAt(r.nextInt(N));
encrypt[15] = randombet.charAt(r.nextInt(N));
encrypt[16] = randombet.charAt(r.nextInt(N));
encrypt[17] = randombet.charAt(r.nextInt(N));
encrypt[18] = randombet.charAt(r.nextInt(N));
encrypt[19] = randombet.charAt(r.nextInt(N));
encrypt[20] = randombet.charAt(r.nextInt(N));
encrypt[21] = randombet.charAt(r.nextInt(N));
encrypt[22] = randombet.charAt(r.nextInt(N));
encrypt[23] = randombet.charAt(r.nextInt(N));
encrypt[24] = randombet.charAt(r.nextInt(N));
encrypt[25] = randombet.charAt(r.nextInt(N));
sentence = "This is the default sentence.";
}
public void randomizeEncrypt()
{
final String randombet = "qwertyuioplkjhgfdsazxcvbnm";
final int N = randombet.length();
Random r = new Random();
for (int i = 0; i < 26; i++)
{
encrypt[i] = randombet.charAt(r.nextInt(N));
}
}
public char [] getAlpha()
{
return alphabet;
}
public char [] getEncrypt()
{
return encrypt;
}
public String getSent()
{
return sentence;
}
public void setSent(String sent)
{
sentence = sent;
}
}
Here is my client application:
import java.util.*;
public class FinalClient
{
static Scanner scan = new Scanner(System.in);
static Final x = new Final();
static char [] testalphabet = new char [26];
public static void main(String[] args)
{
System.out.println("testing encrypted alphabet, in respect to a-z.");
x.randomizeEncrypt();
testalphabet = x.getEncrypt();
for (int i = 0; i < 26; i++) {
System.out.println(testalphabet[i]);
}
System.out.println("Please set sentence to be encrypted.");
x.setSent(scan.next());
String oldSent = x.getSent().toLowerCase();
String newSent = "";
System.out.println("Your encrypted sentence is: \n");
char temp;
for (int i = 0; i < x.getSent().length(); i++)
{
for (int o = 0; o < 26; o++)
{
if (oldSent.charAt(i) != 'a' || oldSent.charAt(i) != 'b' || oldSent.charAt(i) != 'c' || oldSent.charAt(i) != 'd' || oldSent.charAt(i) != 'e' || oldSent.charAt(i) != 'f' ||
oldSent.charAt(i) != 'g' || oldSent.charAt(i) != 'h' || oldSent.charAt(i) != 'i' || oldSent.charAt(i) != 'j' || oldSent.charAt(i) != 'k' || oldSent.charAt(i) != 'l' ||
oldSent.charAt(i) != 'm' || oldSent.charAt(i) != 'n' || oldSent.charAt(i) != 'o' || oldSent.charAt(i) != 'p' || oldSent.charAt(i) != 'q' || oldSent.charAt(i) != 'r' ||
oldSent.charAt(i) != 's' || oldSent.charAt(i) != 't' || oldSent.charAt(i) != 'u' || oldSent.charAt(i) != 'v' || oldSent.charAt(i) != 'w' || oldSent.charAt(i) != 'x' ||
oldSent.charAt(i) != 'y' || oldSent.charAt(i) != 'z')
{
newSent += oldSent.charAt(i);
}
else
{
if (oldSent.charAt(i) == x.getAlpha()[o] )
{
temp = testalphabet[o];
newSent += temp;
}
}
}
}
System.out.println(newSent);
}
}
Thank you ahead of time for any helpful replies.
The problematic area is likely to be on the client section starting on line 19 onwards.