Question: Need help with adjusting my code. Getting a red error for certain problems that have a longer key input from user. It is supposed to

Need help with adjusting my code. Getting a red error for certain problems that have a longer key input from user. It is supposed to display 5021 key but shows 3? Create a class called Cipher. Make the constructor accept some text and a key. Encrypt the given text using the key.
Use the following cipher:
*Take the key and mod it by 26. Example: a key of 30 becomes 4.
*If the character is a letter, shift it by the key, but 'wrap around' the alphabet if necessary.
*If the character is not a letter, then shift it by the key but do not wrap.
Check the test cases for example.
Make getters to support the CipherDemo. Also, make two custom Exceptions called UselessKeyException and EmptyPlainText. In your constructor, throw UselessKeyException if the key is divisible by 26 and throw EmplyPainText if the plain text is zero characters.
My code:
class EmptyPlainText extends Exception {
EmptyPlainText(String s){
super(s);
}
}
class UselessKeyException extends Exception {
private final int key;
UselessKeyException(String s, int key){
super(s);
this.key = key;
}
int getUselessKey(){
return this.key;
}
}
class Cipher {
private String ptext;
private String ctext ="";
private int key;
Cipher(String text, int key) throws EmptyPlainText, UselessKeyException {
if (text.isEmpty()){
throw new EmptyPlainText("Error: Nothing to encrypt!");
}
if (key %26==0){
throw new UselessKeyException("Error: Key is divisible by 26. That's a bad key!", key);
}
this.ptext = text;
this.key = key %26;
for (int i =0; i text.length(); i++){
char ch = text.charAt(i);
if (Character.isLetter(ch)){
char shifted =(char)(ch + this.key);
if (Character.isLowerCase(ch)){
if (shifted >'z') shifted -=26;
} else if (Character.isUpperCase(ch)){
if (shifted >'Z') shifted -=26;
}
ctext += shifted;
} else {
ctext +=(char)(ch + this.key);
}
}
}
String getPlainText(){
return this.ptext;
}
String getCipherText(){
return this.ctext;
}
int getKey(){
return this.key;
}
}
GIVEN CODE:
import java.util.Scanner;
public class CipherDemo {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter some text to encrypt");
String input = keyboard.nextLine();
System.out.println("Enter a key");
int key = keyboard.nextInt();
try {
Cipher c = new Cipher(input, key);
System.out.println("Plain text: "+ c.getPlainText());
System.out.println("Cipher text: "+ c.getCipherText());
System.out.println("Key: "+ c.getKey());
} catch (EmptyPlainText e){
System.out.println(e.getMessage());
} catch (UselessKeyException e){
System.out.println(e.getMessage());
System.out.println("Useless key: "+ e.getUselessKey());
}
}
}
Need help with adjusting my code. Getting a red

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!