The code is compiling but it is not giving the expected output.
Code:
import java.util.Scanner;
public class ProjectTools {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the key");
int key = scan.nextInt();
System.out.println("Enter the text");
String text = scan.next() ;
String x = "aa";
String ciphertext = encrypt( text , key);
System.out.println("ciphertext" + ciphertext);
}
private static String encrypt(String str , int key){
if (key < 0)
{
key = 26-(-key%26);
}
String result = "" ;
for (int i = 0 ; i<str.length() ; i++){
char ch = str.charAt(i);
result+= encryptchar(ch, key);
}
return result;
}
private static char encryptchar(char ch , int key){
if (Character.isUpperCase(ch))
{
return ((char) ( 'A'+ ((ch-'A'+key)%26)));
}
return ch ;
}
}
Re: The code is compiling but it is not giving the expected output.
You forgot to tell us what output you expect, and what you get.
Also, go through Code Conventions for the Java Programming Language: Contents -- your indenting is all over the place.
db
Re: The code is compiling but it is not giving the expected output.
input:
aaa
output:
aaa
but i expect from my code ddd.
Re: The code is compiling but it is not giving the expected output.
Describe what you expect your encryptchar(char ch , int key) to return when ch isn't an uppercase letter.
db
Re: The code is compiling but it is not giving the expected output.
I figure that out.
Thank you.
Re: The code is compiling but it is not giving the expected output.
The following line should be ( 'A'+ ((ch-'A'+key)%26)));
( 'A'+ ((-6 +ch-'A'+key)%26))); if you plug it in the code it will run, and you can try different inputs.
Thanks.