Reputation: 35
My program is supposed to gets a phrase from the user then returns to the user an encrypted code of their choice (either ROT13 or ATBASH) of the phrase they entered. My code compiles and everything and lets the user input the required stuff but when they enter the phrase to be encrypted, nothing happens.. like the new encrypted code doesnt show up, and i dont know what wrong with it! Please help! Thank you!
import java.io.*;
public class J4_1_EncryptionVer4
{
public static void main (String [] args) throws IOException
{
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));//BufferedReader reads user input
//String array letterA[] is initialized
String [][] letterA = new String [][]{
{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},
{"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"},
{"Z","Y","X","W","V","U","T","S","R","Q","P","O","N","M","L","K","J","I","H","G","F","E","D","C","B","A"},
};
System.out.println ("Enter '1' for ROT13 or '2' for ATBASH");//asks user to choose method
String numA = myInput.readLine();//reads user input and assigns it to string
int num = Integer.parseInt (numA);//converts string to integer
int a = 0;//int a is declared
if (num == 1){//if user enters 1
a = 1;//set a to 1
}
if (num == 2) {//end if//if user enters 2
a = 2;//set a to 2
}//end if
System.out.println ( a);
System.out.println(num);
System.out.println ("Please enter a phrase: ");//asks user to enter phrase
String message = myInput.readLine();//reads user input and assigns it to string
int x = 0; //declares int var x
System.out.println ("Your Encrypted code is: ");//prints out scentence
while (x < message.length())//while loop will run while x is less that the phrase length
{
String text = message.toUpperCase();//converts user input to upper case
String letter = Character.toString(text.charAt(x));//extracts character from string and assigns it to another string letter
x++;//increments x by 1 each time
for(int i=0; i<letterA.length; i++)//for loop declares int i = 0, will run while i is less than the the length of the array letterA, and i will increment by 1 each time
{
if(letter.equals(letterA[a][i]))//if the letter is equal to letterA[i]
{
System.out.print (letterA[a][i]);//print out the corresponding letter
break;//breaks from loop
}//end if
else if (letter.equals(" "))//else id the letter is equal to a space
{
System.out.print(" ");//prints out space
break;//breaks from loop
}//end else if
}//end for loop
}//end while loop
}//end main
}//end class
Upvotes: 0
Views: 928
Reputation: 122
I think you should change your for loop to
for (int i= 0; i < letterA[0].length ; i++ ) {
if (letter.equals(letterA[0][i]) {
System.out.print(letterA[a][i]);
break;
}
else {
// .........,.......
}
}
First use the first array as basis. Compare the letters then if they are equal then get the index of that letter to be used in encrypting
I'm using a bloody phone ryt now so I didn't really compile your code
Upvotes: 0
Reputation: 79875
This doesn't work because letterA.length
is 3, so your for
loop only runs through 3 iterations, instead of 26.
Upvotes: 2