Question in caesar encryption code
this code of caesar encryption
have some error ,, pleas help me :confused: Quote:
|
the erroe in this statment
chyfer + = STR.charAt((kay+y)%26); |
This is also homework. :tisk:
|
When you encounter error and want a help, please copy error message here.
|
illegal start expression
|
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.
|
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 ?? |
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 ; }} |
Quote:
Quote:
|
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) |
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"; To localize where you did mistake, java inform you about file and line number where encouter it Quote:
|
Thanks for the clarification, but how to solve this problem ??
|
Change everywhere you have
Code:
for(int i=0; i<=something.length();++i) Code:
for(int i=0; i<something.length();++i) |
it is no diferrent between < and <=
it depend ...... |
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. |