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

Thread: Encrypt a text message

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

    Default Encrypt a text message

    I need to write a code to encrypt any sentence in a square caesar box. So if the sentence has 28 words, the 'box'(array) should have 36 spaces. I should change s.length in my code to something that works this out, but I don't know how.

     
    	public static String encrypt(String plainText) {
     
     
    	String s = plainText.toLowerCase();
    	s = s.replaceAll("\\s","");
    	s.toCharArray();
    	s.charAt(1); // This is the sencence as an array
    	int n = s.length();// This is the size of the box
    	int counter = 0;
    	string [] [] box = new string [n] [];//This is the box
    	for (int i = 0; i<n; i++){
    		for(int j=0; j<n; j++){
    			box[i][j] = s.charAt(counter);
    			counter++;
    		}
    	}
     
     
     
     
     
    	return box;
     
     
     
    	}


    Any ideas?


  2. #2
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Encrypt a text message

    hi
    you dnt need to replace string s .
    instead take n as full string with space. And check whether the string length gives perfect square.If its yes then
    take char[][] box = new char[size][size] where size is the squareroot obtained.Then u can run the loop
    for (int i = 0; i<size; i++){
    for(int j=0; j<size; j++){
    box[j][i] = s.charAt(0);
    }
    }

    then run a loop say taking a string stobtained
    for (int i = 0; i < size; i++)
    {
    for (int j = 0; j < size; j++)
    {
    stobtained += box[i][j];
    }
    }
    System.out.println("string obtained==" + stobtained);
    or return stobtained

    Check this .. hope this solves your problem.

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

    Default Re: Encrypt a text message

    Thanks

    The asssignment requires me to take out blank spaces first

    My main problem is how to proceed when the size is not squareroot, because the code should run for any sentence the user sets up. Any ideas?

    --- Update ---

    ps: perfect square root

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Encrypt a text message

    hi

    so you need to check after removing spaces that if its not a square root and proceed by adding that much space to make it a square root?
    if no then you can jst keep a check like if its not a square root then the print the message that "cannot be encrypted.Not a square root"

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

    Default Re: Encrypt a text message

    It needs to be encypted even if the number of letters is not a square root and in this case there will be extra spaces in the box. I just don't know how to set the size of the box so it will always be a square box even when the number of letters is not a perfect square. Any ideas?

    --- Update ---

    ps:perfect square root

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

    Default Re: Encrypt a text message

    hi
    after removing the spaces if its not a squareroot then keep a check like
    find squareroot of n and round the result up for eg if your string length is 7 then when you find squareroot you will get 2.64.Round this and you will get 3. and square the result.
    Now take the boxsize to the result you obtained when you square the no

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

    Default Re: Encrypt a text message

    Yes, I thought about this pseudocode. I just don't know the correct syntax in Java for it. How to calculate square root?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Encrypt a text message

    How to calculate square root?
    There is a method in the Math class that will do it for you.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Encrypt a text message

    I wanted to write something like if squareroot of n is not an integer then increment n. But the problem is that squareroot must return a double so the way I wrote below won't work.



    [QUOTE]double n = s.length();// This is the size of the sentence
    if(sqrt(n)!=int){
    n++
    }
    /QUOTE]

    How could I fix it?

    --- Update ---

    sorry

    int n = s.length();// This is the size of the sentence
    	if(sqrt(n)!=int){
    	n++	
    	}

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Encrypt a text message

    Take a look at the methods in the Math class. They return values like the whole number above or below their argument. The returned double could be cast to an int
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Encrypt a text message

    I did.


    public static double sqrt(double a)
    Returns the correctly rounded positive square root of a double value.

    So it always returns a rounded double. Does it mean it might round down so I will get less spaces than s.length()?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Encrypt a text message

    when you cast the double to an int, is it the correct value?
    To test your use of the Math methods, write a loop that goes from below a square (say 33) to above a square (say 38) print the number and the value your code returns. For example:
    33 6
    34 6
    35 6
    36 6
    37 7
    38 7
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Encrypt a text message

    No, the code below returns error: Syntax error on token "int", invalid Expression

    int n = s.length();// This is the size of the sentence
    	if(sqrt(n)!=int){
    	n++;	
    	}

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Encrypt a text message

    token "int", invalid Expression
    The compiler says that the use of the keyword int at that location is invalid.
    The expression/variable to the right of the != operator needs to be the same data type as the expression to the left of the != operator. As the saying goes: You can't compare apples with oranges.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Encrypt a text message

    Yes, sqrt(n) is a double, but how could I test if this double will be perfect square root if it "Returns the correctly rounded positive square root of a double value" ?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Encrypt a text message

    Take a look at the methods in the Math class. They return values like the whole number above or below their argument. The returned double could be cast to an int.

    The arg to the method would be the double returned by the sqrt() method.

    Returns the correctly rounded positive square root of a double value" ?
    Write a test loop that prints out the loop index and the value returned by sqrt() for values from 33 to 38.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Encrypt a text message

    I have tried for only one input of 27 letters and it is not working. I want it to return 36 and it is returning 33, because when n gets to 33 the square root is 6 and the if test is not satisfied anymore. How can I make it not round up the square root?

    double n = s.length();// This is the size of the sentence
     
    	double sqrt = (double) Math.sqrt(n);// This is the square root of number of letters
     
    	if(sqrt*sqrt!=n){
    	n++;
    	}

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Encrypt a text message

    Did you miss this:
    Take a look at the methods in the Math class. There some that return values like the whole number above or below their argument.

    Are you getting this: warning: [cast] redundant cast to double
    double sqrt = (double) Math.sqrt(n);
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Encrypt a text message

    No, I did not get this error. And I tried with int as well

    int n = s.length();// This is the size of the sentence
     
    	int sqrt = (int) Math.sqrt(n);// This is the square root of number of letters
     
    	if(sqrt*sqrt!=n){
    	n++;
    	}
     
        System.out.println(n);

    I need to find a way sqr won't round up because this is the reason why n is not a number with perfect square root.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Encrypt a text message

    I need to find a way sqr won't round up
    Take a look at the methods in the Math class. There some that return values like the whole number above or below their argument. The arg would be the value returned by sqrt()
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [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
  2. Replies: 2
    Last Post: June 4th, 2012, 11:40 PM
  3. [SOLVED] Unable to encrypt using AES 256bits
    By fuweng in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 5th, 2011, 07:44 AM
  4. How do i encrypt my password at command line
    By AHOT in forum Java Theory & Questions
    Replies: 5
    Last Post: October 13th, 2010, 03:13 PM
  5. Need Help for encrypt/decrypt.
    By superdhebz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 17th, 2010, 12:17 PM