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.

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

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"

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

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

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?

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.

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 sentence
if(sqrt(n)!=int){
n++
}

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

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()?

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

Re: Encrypt a text message

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

Code java:

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

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.

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

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.

Quote:

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.

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 sentence
double sqrt = (double) Math.sqrt(n);// This is the square root of number of letters
if(sqrt*sqrt!=n){
n++;
}

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

Re: Encrypt a text message

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

Code java:

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.

Re: Encrypt a text message

Quote:

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()