Hey if someone could help me shed some light on my code as too why it is getting the error String index out of range: -1 i would be for ever in your debt
the part of the question im stuck on is this
"At the beginning, your program should ask the user to specify a one-to-one mapping between the
characters in the above mentioned alphabet. For example, your program may display the 40 characters
in one line and ask the user to type the same set of characters in the second line, but in a different
order. After the mapping is established, your program should then ask the user to type in a sentence
using the characters from this alphabet, and then output the encrypted sentence"
The bit in red is the bit im really stuck on
Code:
package question3;
import java.util.Scanner;
public class Question3 {
private String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ,.!";
private String MAP = "";
private String Pass = "";
Scanner kb = new Scanner(System.in);
public void getString(){
System.out.println("These are the Characters that are able to be used");
System.out.println(alpha);
System.out.println("Please input encryption");
MAP = kb.nextLine();
MAP = MAP.toUpperCase();
}
public void checkalpha(){
int firstIndex = 0;
int lastIndex = 0;
int count = 1;
boolean repeatedChars = false;
if(MAP.length() == 40){
for (int i = 0; count < MAP.length(); i++){
char character = MAP.charAt(i);
firstIndex = MAP.indexOf(character);
lastIndex = MAP.lastIndexOf(character);
if(firstIndex != lastIndex) {
repeatedChars = true;
}
count++;
}
if(repeatedChars == true) {
System.out.println("There were repeated characters in the string");
System.out.println("Please Do not Input the characters more than Once");
}
else {
System.out.println("No Repeated Characters");
}
if (MAP.equals(alpha)){
System.out.println("Please Do not repeat the starting characters");
getString();
}
}
else {
System.out.println("Incorrect Amount of Characters");
System.out.println("");
}
}
public void printConverted(){
char PassIndex;
System.out.println("Please Input message wanted to be converted");
Pass = kb.nextLine();
for (int i = 0; i < Pass.length(); i++){
PassIndex = MAP.charAt(Pass.indexOf(alpha.charAt(i)));
System.out.println(PassIndex);
}
}
printConverted method is the one with the problem, im positive about that.
Thanks for taking a look, if you need more info about something please let me know, i am very new to java so i really want to learn what im doing wrong