Results 1 to 3 of 3
Thread: Exceptions Question
- 02-18-2013, 08:28 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
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
Java 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"); } } }
- 02-18-2013, 08:35 PM #2
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
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.
- 02-19-2013, 10:42 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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.Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads
-
Catching Exceptions (quick question)
By Danieldcc in forum New To JavaReplies: 3Last Post: 02-20-2012, 12:40 AM -
WOW need help with Exceptions
By starplayerrob in forum New To JavaReplies: 4Last Post: 12-12-2011, 11:49 AM -
Exceptions
By hedonist in forum New To JavaReplies: 10Last Post: 09-08-2009, 09:38 AM -
question regarding exceptions..
By SCS17 in forum New To JavaReplies: 3Last Post: 11-17-2007, 10:31 AM -
Question on Exceptions
By yelllow4u in forum New To JavaReplies: 6Last Post: 07-27-2007, 02:41 PM
Bookmarks