I'm trying to create an encryption scheme that replaces the letters in a String with a corresponding letter.
For example, in the string "apple", the "a" is replaced with "k", and so on. Each letter has a fixed corresponding letter.
I want to get user input and store it into an array.
Then I want to loop through the array and find each index of the String. Then replace each index with the corresponding letter.
Here's what I cooked up so far but I'm unable to make the code run. I'm mainly getting, error: incompatible types.
I can't determine whether I should be using the charAt method and changing my variable types to char.
import java.util.*;
public class Encrypt {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the String Name to encrypt:");
String inputString = input.nextLine();
String[] str = new String[inputString];
replaceString();
}
public static void replaceString() {
for(int i = 0; i < str.length(); i++) {
if(str.indexOf(i) == "a") {
str.indexOf(i) = "k";
} else if(str.indexOf(i) == "b") {
str.indexOf(i) = "n";
}
//and so on A-Z...
System.out.print(str);
}
}
}
str.indexOf(i)returns anint, not aString, you can usestr.charAtwhich will returncharinstead, which might be more pratical.==isn't how you compareStrings in Java