Home > Blockchain >  How to mod a string when out of bounds?
How to mod a string when out of bounds?

Time:04-21

What this does is you get to choose the letters you want to pick from the String Choice and then they get converted by the number chosen in the integer converter. How do I do it so that when it reaches out of bounds it can go back to the first value, which in this case is "F"?

String Letters = "FEDCBA9876543210";

int converter;
Scanner lire = new Scanner(System.in);
System.out.println("hint: FEDCBA9876543210\n");
System.out.print("message?: ");
message = lire.nextLine();

System.out.print("Delta no?: ");
converter = lire.nextInt();
char test[] = message.toCharArray();
int check;
System.out.print("resultat: ");
//Convertisseur ROT
for (int i = 0; i < message.length(); i  ){
    test[i] = Choice.charAt(i  converter);
    check = i  converter;

    System.out.print(test[i]);
}

CodePudding user response:

I think I don't understand the whole question but maybe this can help you:
instead of Choice.charAt(i converter) you can try to use the remainder of the division by the length of the array, that is Choice.charAt((i converter)%Choice.lenght); so when the index goes out of bounds it return to the first element.

  • Related