Results 1 to 3 of 3
  1. #1
    Clown is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default 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");
    	}
    }
    
    }

  2. #2
    Clown is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default 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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default 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

  1. Catching Exceptions (quick question)
    By Danieldcc in forum New To Java
    Replies: 3
    Last Post: 02-19-2012, 11:40 PM
  2. WOW need help with Exceptions
    By starplayerrob in forum New To Java
    Replies: 4
    Last Post: 12-12-2011, 10:49 AM
  3. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 08:38 AM
  4. question regarding exceptions..
    By SCS17 in forum New To Java
    Replies: 3
    Last Post: 11-17-2007, 09:31 AM
  5. Question on Exceptions
    By yelllow4u in forum New To Java
    Replies: 6
    Last Post: 07-27-2007, 01:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •