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.

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

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

April 12th, 2013, 01:34 AM

hope_08

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"

April 12th, 2013, 01:46 AM

dianac

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

April 12th, 2013, 06:31 AM

hope_08

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

April 12th, 2013, 06:40 PM

dianac

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?

April 12th, 2013, 06:41 PM

Norm

Re: Encrypt a text message

Quote:

How to calculate square root?

There is a method in the Math class that will do it for you.

April 12th, 2013, 07:07 PM

dianac

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

Code java:

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

April 12th, 2013, 07:15 PM

Norm

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

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

April 12th, 2013, 07:36 PM

Norm

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

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

April 12th, 2013, 07:50 PM

Norm

Re: Encrypt a text message

Quote:

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.

April 12th, 2013, 08:06 PM

dianac

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" ?

April 12th, 2013, 08:12 PM

Norm

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.

Write a test loop that prints out the loop index and the value returned by sqrt() for values from 33 to 38.

April 12th, 2013, 08:50 PM

dianac

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?

Code java:

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

April 12th, 2013, 08:52 PM

Norm

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.