• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cant find solution - OutOfBoundsException

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the logic figured out but I can't seem to get past the implementation.

Thanks for any help

Rob



This is the error I get:

OutOfBoundsException: 27
at Cipher.search(Cipher.java:31)
at Cipher.encrypt(Cipher.java:73)
at Cipher.main(Cipher.java:89)



CODE:


public class Cipher {

public static int search (char[]g, char lookfor, int n) {

boolean found;
int i;

found = false;
i=0;


do {
if (lookfor == g[i]) {
found = true;
}
else {
i++;
}
} while (!found && 1 < n);
if (found) {
return i;
}
else {
return -1;
}
}

public static String encrypt (String str) {

String plainText;
String cipherText;
String outString;
char findChar;
char foundChar;
int foundPos;

char[]p = new char[27];
char[]c = new char[27];

plainText = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cipherText = "}{+)(_*^&$%!#@~[;]:?><,./|`";
outString = "";
for (int i=0; i <= 26; i++) {
p[i] = plainText.charAt(i);
c[i] = cipherText.charAt(i);
}
for (int n=0; n <= str.length(); n++) {
findChar = str.charAt(n);
foundPos = search(p, findChar, 26);
outString = outString + foundPos;
}
return outString;
}


// main line of program
public static void main (String[] args) {
String plainText;
String cipherText;
plainText = "abcdefg";
cipherText = encrypt(plainText);
System.out.println(cipherText);
}
}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
perhaps
} while (!found && 1 < n);

should be
} while (!found && i < n);


and
for (int n=0; n <= str.length(); n++) {

should be
for (int n=0; n < str.length(); n++) {
 
Robert Hopping
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your assistance. I can never find my own typos (1 instead of i). Everything is now working.
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic