LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Question in caesar encryption code (https://www.linuxquestions.org/questions/programming-9/question-in-caesar-encryption-code-4175450960/)

nawal1991 02-20-2013 09:22 AM

Question in caesar encryption code
 
this code of caesar encryption

have some error ,, pleas help me :confused:


Quote:

package encryption;
import java.util.*;
/**
*
* @author Toshiba
*/
public class Encryption {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ENCR n =new ENCR();
Scanner S = new Scanner(System.in);
System.out.print(" Enter the text");
String massege = S.next();
System.out.print(" Enter the kay");
String key = S.next();


System.out.print(" the word chfre is :" + n.NN );
}
}
public class ENCR{

public ENCR () {}
public String NN ( String massege , String kay )

{

String STR= "ABCDEFGHIJKLMNOPQRSTWXYZ" ;
char current;
String chyfer= null;
for (int i=0; i<=massege .length();i++)
{

current = massege .charAt(i);

for (int y= 0 ; y<=STR.length(); y++) {
if(current==STR.charAt(y))
{

chyfer + = STR.charAt((kay+y)%26);

return chyfer ;

}
}
}
return chyfer ;
}
}

nawal1991 02-20-2013 09:26 AM

the erroe in this statment


chyfer + = STR.charAt((kay+y)%26);

sundialsvcs 02-20-2013 09:30 AM

This is also homework. :tisk:

eSelix 02-20-2013 10:09 AM

When you encounter error and want a help, please copy error message here.

nawal1991 02-20-2013 10:38 AM

illegal start expression

eSelix 02-20-2013 11:20 AM

Compiler said to you about "+ =" that it don't known what do you mean by addiding "=" sign. If you want to use symbol for "add and assign" it is "+=". Without space between.

nawal1991 02-20-2013 12:26 PM

thanks eSelix , it was done success

..

--------

when i want to call the method NN in the main ,,



i should to create an object ,, it is true ??


if it is true create an object from what ??

nawal1991 02-20-2013 03:26 PM

After modified the mistakes got the following code, but it is not run >>


What is the reason ؟؟

package encryption;
import java.util.*;
public class Encryption {
public static void main(String[] args) {
Encryption enc = new Encryption();
Scanner S = new Scanner(System.in);
System.out.println(" Enter the text");
String massege = S.next();
System.out.println(" Enter the kay");
int key = S.nextInt();

System.out.println( " the word chyfer is " + enc.NN( massege, key) );
}
public String NN ( String massege , int kay )
{
String STR= "ABCDEFGHIJKLMNOPQRSTWXYZ" ;
char current;
String chyfer= "";
for (int i=0; i<=massege .length();i++)
{
current = massege .charAt(i);
for (int y= 0 ; y<=STR.length(); y++) {
if(current==STR.charAt(y))
{
chyfer+=STR.charAt((kay+y)%26);}
}}
return chyfer ;
}}

eSelix 02-21-2013 06:02 AM

Quote:

Originally Posted by nawal1991 (Post 4896234)
when i want to call the method NN in the main...

If you want to access a method belonging to some class, then, depending on from where you want to call it, if from the same class, then you do not create additional object, because you are already in some object (of course you create it if you want to use another instance). If you access it from outside this class where it is defined, then you must use some object on which this method will be working, so you need to create it somewhere. Other rules are when using static methods.


Quote:

After modified the mistakes got the following code, but it is not run
What do you mean by "not run"? Please paste how you are trying to run it, an error messages, etc.

nawal1991 02-21-2013 06:28 AM

when i run the code ,, it Asks me to enter the text and key ..


but it does not show the word chyfer !!!

like this :

Enter the text
jg
Enter the kay
2
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 24
at java.lang.String.charAt(String.java:686)
at encryption.Encryption$Encryp.NN(Encryption.java:28)
at encryption.Encryption.main(Encryption.java:13)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

eSelix 02-21-2013 08:06 AM

By "Exception" java inform you about error during execution of your program. For example "StringIndexOutOfBoundsException: String index out of range" means that you tried to get a character from string, but form position where are no more characters. For example
Code:

String test = "abcd";
char some_var = test.charAt(7);

As this string had only 4 characters you can't get 7th, it is bad error in Java. You need to remember that characters in string are counted from 0. So in above example you can use indexes 0, 1, 2 and 3 (4 letters), but not index 4 because it would be 5th letter, which not exists in that string.

To localize where you did mistake, java inform you about file and line number where encouter it
Quote:

at encryption.Encryption$Encryp.NN(Encryption.java:28)
File "Encryption.java" line 28. Look there, you probably see a "charAt()" function call with some argument. To debug your program you can print out this value and compare it with possible indexes you can use on that particular string.

nawal1991 02-21-2013 09:29 AM

Thanks for the clarification, but how to solve this problem ??

eSelix 02-21-2013 10:02 AM

Change everywhere you have
Code:

for(int i=0; i<=something.length();++i)
to
Code:

for(int i=0; i<something.length();++i)
You really should learn Java if you want to program in it.

nawal1991 02-21-2013 02:43 PM

it is no diferrent between < and <=


it depend ......

eSelix 02-22-2013 05:26 AM

Well, it is. If for example "something.length()" is equal 3, then "<" do that variable "i" will get 0, 1, 2 and when you use "<=" then "i" will be 0, 1, 2, 3 and in last loop you will get "something.charAt(3)" therefore you get "out of bounds" exception.


All times are GMT -5. The time now is 12:11 PM.