LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-20-2013, 09:22 AM   #1
nawal1991
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Rep: Reputation: Disabled
Lightbulb Question in caesar encryption code


this code of caesar encryption

have some error ,, pleas help me


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 ;
}
}
 
Old 02-20-2013, 09:26 AM   #2
nawal1991
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Original Poster
Rep: Reputation: Disabled
the erroe in this statment


chyfer + = STR.charAt((kay+y)%26);
 
Old 02-20-2013, 09:30 AM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,330

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
This is also homework.
 
Old 02-20-2013, 10:09 AM   #4
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,211

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
When you encounter error and want a help, please copy error message here.
 
Old 02-20-2013, 10:38 AM   #5
nawal1991
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Original Poster
Rep: Reputation: Disabled
illegal start expression
 
Old 02-20-2013, 11:20 AM   #6
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,211

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
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.
 
Old 02-20-2013, 12:26 PM   #7
nawal1991
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Original Poster
Rep: Reputation: Disabled
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 ??
 
Old 02-20-2013, 03:26 PM   #8
nawal1991
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Original Poster
Rep: Reputation: Disabled
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 ;
}}
 
Old 02-21-2013, 06:02 AM   #9
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,211

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
Quote:
Originally Posted by nawal1991 View Post
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.
 
Old 02-21-2013, 06:28 AM   #10
nawal1991
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Original Poster
Rep: Reputation: Disabled
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)
 
Old 02-21-2013, 08:06 AM   #11
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,211

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
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.
 
Old 02-21-2013, 09:29 AM   #12
nawal1991
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Original Poster
Rep: Reputation: Disabled
Thanks for the clarification, but how to solve this problem ??
 
Old 02-21-2013, 10:02 AM   #13
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,211

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
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.
 
Old 02-21-2013, 02:43 PM   #14
nawal1991
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Original Poster
Rep: Reputation: Disabled
it is no diferrent between < and <=


it depend ......
 
Old 02-22-2013, 05:26 AM   #15
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,211

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Encryption question prasanna.R Linux - Newbie 12 12-09-2010 08:02 AM
HDD Encryption Question richinsc Linux - Security 2 09-24-2008 06:17 AM
Encryption question jantman Linux - Security 5 07-20-2006 02:25 PM
Caesar Cypher algorithm (JAVA) randomx Programming 4 10-05-2003 10:16 PM
question on SSL, encryption sopiaz57 Linux - General 1 09-10-2003 02:54 PM


All times are GMT -5. The time now is 08:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration