Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to mod a string when out of bounds?

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]);
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading