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 22 of 22

Thread: Encrypting a text message

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

    Default Encrypting a text message

    I need to encrypt a text message in a two dimensional array(closest square matrix to number of letters in the text message) putting each letter that comes first in the first row, then second row... etc

    It is not working and I don't understand the error messages.

    public class CaesarBox {
     
    	public static void main(String[] args) {
    		// CaesarsBox <-encrypt|-decrypt> 
     
    		if(args[0].equals("-encrypt")){
    			System.out.println(encrypt(args[1]));
    		}
     
    	}
     
    	public static String encrypt(String plainText) {
     
     
    	String s = plainText.toLowerCase();// Taking out upper cases
     
    	s = s.replaceAll("\\s","");//Taking out blank spaces
     
    	 char [] Arr = s.toCharArray();//Convert from string to array of characters
     
    	float squareRoot = (float) Math.sqrt(s.length());
     
    	int dimension = (int) Math.ceil(squareRoot);
     
    	int x = dimension;
     
    	char[][] box = new char [x][x];// This is the box: a 2 dimensional array
     
    	int counter = 0;
     
    	for(int i = 0; i<x; i++){
     
    		for(int j = 0; j<x; j++){
     
    				box[i][j]= Arr[counter];
     
    				counter++;
    			}
    		}
     
     
        System.out.print(box);
     
    	return s;
    	}	
    }


    Errors:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
    at CaesarBox.encrypt(CaesarBox.java:39)
    at CaesarBox.main(CaesarBox.java:11)


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Encrypting a text message

    Quote Originally Posted by dianac View Post
    It is not working and I don't understand the error messages.
    Any time you have an exception, type the exception into your favorite search engine. Once you understand java.lang.ArrayIndexOutOfBoundsException look at the code again for what may be the cause.
    Ask more questions if necessary.

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypting a text message

    Where is favorite search engine? I use Eclipse

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Encrypting a text message

    doesnt he mean a search engine such as google ?

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Encrypting a text message

    Quote Originally Posted by mist4lyf View Post
    doesnt he mean a search engine such as google ?
    Yes, I apologize for the confusion.

    java.lang.ArrayIndexOutOfBoundsException
    ...indicate that an array has been accessed with an illegal index...

  6. #6
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypting a text message

    Thanks, but I have been looking to the code and can't see what this out of bounds is.

    I have modified the code to:

    public class CaesarBox {
     
    	public static void main(String[] args) {
    		// CaesarsBox <-encrypt|-decrypt> 
     
    		if(args[0].equals("-encrypt")){
    			System.out.println(encrypt(args[1]));
    		}
     
    	}
     
    	public static String encrypt(String plainText) {
     
     
    	String s = plainText.toLowerCase();// Taking out upper cases
     
    	s = s.replaceAll("\\s","");//Taking out blank spaces
     
    	float squareRoot = (float) Math.sqrt(s.length());
     
    	int dimension = (int) Math.ceil(squareRoot);
     
    	int x = dimension;
     
    	char[][] box = new char [x][];// This is the box: a 2 dimensional array
     
    	int counter = 0;
     
    	for(int i = 0; i<x; ++i){
     
    		for(int j = 0; j<x; ++j){
     
    				box[i][j]= s.charAt(counter);
     
    				counter++;
    			}
     
    		}
     
     
        System.out.print(box);
     
    	return s;
    	}	
    }


    The problem is in:box[i][j]= s.charAt(counter);

    And I don't get how this is out of bounds. Can anyone help me see it?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypting a text message

    Add a println statement just before the statement where the error happens that prints out the values of the indexes: i, j and counter.
    Also print out the size of the arrays that are being indexed: box, box[i] and s
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypting a text message

    Thanks, fixed it. The problem now is the size of the box keeps showing up as 6 and it should be a 2 dimensional array 6 x 6

     
    public class CaesarBox {
     
    	public static void main(String[] args) {
    		// CaesarsBox <-encrypt|-decrypt> 
     
    		if(args[0].equals("-encrypt")){
    			System.out.println(encrypt(args[1]));
    		}
     
    	}
     
    	public static String encrypt(String plainText) {
     
     
    	String s = plainText.toLowerCase();// Taking out upper cases
     
    	s = s.replaceAll("\\s","");//Taking out blank spaces
     
    	float squareRoot = (float) Math.sqrt(s.length());
     
    	int dimension = (int) Math.ceil(squareRoot);
     
    	int x = dimension;
     
    	int y = dimension;
     
    	char[][] box = new char [x][y];// This is the box: a 2 dimensional array
     
    	System.out.println(box.length);
     
    	int counter = 0;
     
    	for(int i = 0; i<x; i++){
     
    		for(int j = 0; j<y; j++){
     
               box[i][j]= s.charAt(counter);
     
    				if(counter<31){
    					counter++;
     
    					System.out.println(box[i][j]);
    				}
    			}
     
    		}
     
    	System.out.println(box);
     
    	return s;
    	}	
    }

    What could be wrong?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypting a text message

    size of the box keeps showing up as 6
    box is an array of arrays. Its size is 6
    box[i] is one of the arrays that is in the box array. What was its size?

    What were the other values requested in post#7 that were printed out?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypting a text message

    box[i] is 6
    s size is 32

    The problem is that box is printing a one dimensional array

    --- Update ---

    is being printed as one dimensional array

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypting a text message

    s size is 32
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
    The max index for s is 31. The error message says there was an index with a value of 32.
    What printed out for the value of counter?

    The problem is that box is printing a one dimensional array
    Post the output and the statement that is making it. Why do you say "one dim array"?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypting a text message

    The exception was already fixed.


    i say one dimension array, because it is printing all in one row

     
     
     
    public class CaesarBox {
     
    	public static void main(String[] args) {
    		// CaesarsBox <-encrypt|-decrypt> 
     
    		if(args[0].equals("-encrypt")){
    			System.out.println(encrypt(args[1]));
    		}
     
    	}
     
    	public static String encrypt(String plainText) {
     
     
    	String s = plainText.toLowerCase();// Taking out upper cases
     
    	s = s.replaceAll("\\s","");//Taking out blank spaces
     
    	float squareRoot = (float) Math.sqrt(s.length());
     
    	int dimension = (int) Math.ceil(squareRoot);
     
    	int x = dimension;
     
    	int y = dimension;
     
    	char[][] box = new char [x][y];// This is the box: a 2 dimensional array
     
     
    	int counter = 0;
     
    	for(int i = 0; i<x; i++){
     
    		for(int j = 0; j<y; j++){
     
               box[i][j]= s.charAt(counter);
     
    				if(counter<31){
    					counter++;					
    				}
    				System.out.print(box[i][j]);	
    			}
     
    		}
     
     
     
    	return s;
    	}	
    }

    printing box[i][j] is returning
    alongtimeagoinagalaxyfar,farawayyyyyalongtimeagoin agalaxyfar,faraway



    and args[1] is "A long time ago in a galaxy far, far away"

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypting a text message

    Is the code working now? If you are having problems, please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypting a text message

    It is working but the output is not the desired.

    Box should be printing

    a l o n g t
    i m e a g o
    , i n a g a
    l a x y f a
    r , f a r a
    w a y

    and it is printing alongtimeagoinagalaxyfar,farawayyyyyalongtimeagoin agalaxyfar,faraway

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypting a text message

    You need to add line end character at the place in the output where you want the following characters to go to a new line. The print() method will print everything on the same line.
    Call the println() method when you want the next characters to go on the next line.

    The extra String printed at the end is from this println:
    System.out.println(encrypt(args[1]));
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypting a text message

    What if I want to print box each row in one line?

    The is dued to return s in the code. This method requires me to return a string. But when I try to return box it does not allow me, because it is not a string.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypting a text message

    What if I want to print box each row in one line?
    Call the println() at the end of each row to put the next row on a new line.

    The encrypt method should return the encrypted String, not the array it uses to build the String.
    Go back and read the assignment.

    I'm done for tonight.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Cannot return string in the method

    I need to return the encrypted string called box, but it keeps showing error

    public class CaesarBox {
     
    	public static void main(String[] args) {
    		// CaesarsBox <-encrypt|-decrypt> 
     
    		if(args[0].equals("-encrypt")){
    			System.out.println(encrypt(args[1]));
    		}
     
    	}
     
    	public static String encrypt(String plainText) {
     
     
    	String s = plainText.toLowerCase();// Taking out upper cases
     
    	s = s.replaceAll("\\s","");//Taking out blank spaces
     
    	float squareRoot = (float) Math.sqrt(s.length());
     
    	int dimension = (int) Math.ceil(squareRoot);
     
    	int x = dimension;
     
    	int y = dimension;
     
    	char[][] box = new char [x][y];// This is the box: a 2 dimensional array
     
     
    	int counter = 0;
     
    	for(int i = 0; i<x; i++){
     
    		for(int j = 0; j<y; j++){
     
               box[i][j]= s.charAt(counter);
     
    				if(counter<31){
    					counter++;	
    				}	
    			}
     
    		}
    	System.out.println( java.util.Arrays.deepToString(box));
     
    	return box ;
    	}
     
    }

  19. #19
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot return string in the method

    is there a full solution to this problem? i'm struggling so bad

  20. #20
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Wrong output

    The output should be

    a l o n g t
    i m e a g o
    , i n a g a
    l a x y f a
    r , f a r a
    w a y

    and it is returning

    [[a, l, o, n, g, t], [i, m, e, a, g, o], [i, n, a, g, a, l], [a, x, y, f, a, r], [,, f, a, r, a, w], [a, y, y, y, y, y]]


    public class CaesarBox {
     
    	public static void main(String[] args) {
    		// CaesarsBox <-encrypt|-decrypt> 
     
    		if(args[0].equals("-encrypt")){
    			System.out.println(encrypt(args[1]));
    		}
     
    	}
     
    	public static String encrypt(String plainText) {
     
     
    	String s = plainText.toLowerCase();// Taking out upper cases
     
    	s = s.replaceAll("\\s","");//Taking out blank spaces
     
    	float squareRoot = (float) Math.sqrt(s.length());
     
    	int dimension = (int) Math.ceil(squareRoot);
     
    	int x = dimension;
     
    	int y = dimension;
     
    	char[][] box = new char [x][y];// This is the box: a 2 dimensional array
     
     
    	int counter = 0;
     
    	for(int i = 0; i<x; i++){
     
    		for(int j = 0; j<y; j++){
     
               box[i][j]= s.charAt(counter);
     
    				if(counter<31){
    					counter++;	
     
    				}	
     
    			}
     
    		}
    	String encrypt = java.util.Arrays.deepToString(box);
     
    	return encrypt ;
    	}
     
    }


    What could be wrong?

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Cannot return string in the method

    I need to return the encrypted string
    The variable: box is not a String. You need to look at the assignment to see what the method should return.


    NOTE: All posts for this topic have been merged.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    dianac (April 14th, 2013)

  23. #22
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypting a text message

    I know, just was looking how to convert it to string. Already solved, thanks.

Similar Threads

  1. Encrypt a text message
    By dianac in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 12th, 2013, 09:32 PM
  2. [SOLVED] One string Text message into array of strings(letters)
    By dianac in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 12th, 2013, 07:23 PM
  3. encrypting and decrypting a string
    By mist4lyf in forum What's Wrong With My Code?
    Replies: 17
    Last Post: April 9th, 2013, 08:41 AM
  4. my encrypting simple client to server program unable to get the key from client
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 07:15 AM
  5. Replies: 0
    Last Post: October 29th, 2011, 02:37 AM