-
Exceptions Question
Hi,
Here is the following class that I created, it implements a simple Caesar cipher. It encrypts a String message. The Input should contain only small letters.
In my method checkMessage I check whether the String is correct. If not I would like to throw an Exception. For this purpose i have created a class InputException that prints an error message.
Unfortunately when I run the program I get the following Output:
Please enter a valid string
InputException
at Encrypt.checkMessage(Encrypt.java:47)
at Encrypt.main(Encrypt.java:62)
I understand that I handle the same Exception twice which is probably not right. I want to handle the exception in my checkMessage method so that the programm does not continue after that.
Is my ExceptionClass ok or should I add something to it?
Any advice and suggestions would be greatly appreciated.
Best,
Yordan
Code:
public class Encrypt { // The class is a simple Caesar cypher
private int key; //key
private String message; //message to be encypted, only small letters allowed
public Encrypt(int key, String message){
this.key=key;
this.message=message;
}
public int getKey() {
return key;
}
public void setKey(int key) { //setting key in case it is >26
this.key = key%26;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public char encryptChar(char x){ //encrypts a char
if(x==' '){
return ' ';
}
else if (x+this.key-97<26){
return (char) (x+this.key);
}
else {
return (char)(x+this.key-26);
}
}
public void checkMessage()throws InputException{ //checks if the input is valid
for (int i = 0; i < message.length(); i++) {
if(message.substring(i, i+1).matches("[a-z]")||(message.substring(i, i+1)).trim().length()==0){
}
else throw new InputException();
}
}
public void printencrpted(){ //simple print method
for (int i = 0; i < message.length(); i++) {
System.out.print(encryptChar(message.charAt(i)));
}
}
public static void main(String[] args) throws InputException {
try{
Encrypt ent=new Encrypt(27, "zaz2a");
ent.setKey(ent.key);
ent.checkMessage();
ent.printencrpted();
}
catch (InputException e){
e.printStackTrace();
}
}
public class InputException extends RuntimeException {
public InputException(){
System.out.println("Please enter a valid string");
}
}
}
-
Re: Exceptions Question
Ok, I figured that the error message is the stack trace printed. But I would still appreciate any suggestions as to how I can make my code better.
-
Re: Exceptions Question
The basic structure is fine for the Encrypt class, however I would not make the "user" have to call a method in order to validate the String to be encrypted.
It should be part of some encrypt() method.
By "user" I mean any code that calls this.
I would also move you main() method out into its own class. That way you can see what Encrypt looks like without the extra fluff caused by having a mian() method in there.
Do you really need getters and setters for the message attribute?
The user is already supplying the message in the constructor.
Same with the key...speaking of which you are doing some logic on the key in the setter that is not being done in the constructor.
encryptChar should probably be private. That looks like the inner workings of the class so shouldn't be visible from outside.
As I said above, I would have an encrypt method that returns the encrypted String (after validating) and remove the print() method. Have the user do the printing. It doesn't strike me as something the Encrypter should be doing.
A lot of these are more a good practice thing, like visibilty, and where to put things.