Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: caesar box with length of non-square...

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default caesar box with length of non-square...

    public static String encrypt(String plainText) {
    int a = plainText.length();
    // Determining the size of the Array
    // Rounding up square root of text length
    int SizeOfArray = ((int) Math.ceil(Math.sqrt(a)));

    // Setting the size of the Array
    char[][] encryptArray = new char[SizeOfArray][SizeOfArray];

    for (int i = 0; i < SizeOfArray; i++) {
    for (int j = 0; j < SizeOfArray; j++) {
    encryptArray[i][j] = plainText.charAt(0);
    plainText = plainText.substring(1);
    }
    }

    String putTogether = "";

    for (int i = 0; i < SizeOfArray; i++) {
    for (int j = 0; j < SizeOfArray; j++) {
    putTogether += encryptArray[j][i];
    }
    }
    return putTogether;
    }

    The code works perfectly with perfect square length

    but it gives error with non square length..
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:686)

    Please Help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: caesar box with length of non-square...

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:686)
    The code at line 686 is using an index past the end of the String. The index is shown as 0 which means that the String must be empty. The code should test the length of the String before trying to index into it to make sure there are characters in the String.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Making an input dialog box appear after closing a combo box
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 14
    Last Post: November 11th, 2012, 06:22 PM
  2. Caesar Encryption
    By andrew_m428 in forum The Cafe
    Replies: 1
    Last Post: October 4th, 2012, 10:59 PM
  3. [SOLVED] Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 16
    Last Post: November 19th, 2011, 04:18 PM
  4. What is a non-zero square?
    By djl1990 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 30th, 2011, 03:11 PM
  5. Encode and decode - Caesar cipher
    By siabanie in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2011, 06:25 PM