Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Norm

Thread: How to generate and use variables in different switch statements

  1. #1
    YellowSun is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default How to generate and use variables in different switch statements

    I'm trying to encrypt a text with DES algorithm using Bouncy Castle library. I have written the following code:

    Java Code:
        	
        	try{
         		KeyGenerator kg=KeyGenerator.getInstance("DES");
         		SecretKey key=kg.generateKey();
         		SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "DES");
         		Cipher cipher=Cipher.getInstance("DES");
        	}catch(Exception e)
        	{
        		
        	}
        	
        	  	
        	switch(selection){
        	
        	case enc:
        		try{
        			
        			cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        			String inText=inputText.toString();
        			byte[] cipherText=cipher.doFinal(inText.getBytes());
        			encText.setText(new String(cipherText));
        		}catch(Exception e)
    			{
    			encText.setText("Exception occured!");	
    			}
        		break;
        		
        	case dec:
        		try{
        			cipher.init(Cipher.DECRYPT_MODE, keySpec);
        			String encT=encText.toString();
        			byte[] decBytes=cipher.doFinal(encT.getBytes());
        			decText.setText(new String(decBytes));
        		}catch(Exception e)
    			{
    			decText.setText("Exception occured!");	
    			}
        		break;
        	}
        }
    The code is supposed to work as follows: The following part of the code will generate "keySpec" and "Cipher" which is used in both encryption and decryption parts in the switch statement.

    Java Code:
    try{
         		KeyGenerator kg=KeyGenerator.getInstance("DES");
         		SecretKey key=kg.generateKey();
         		SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "DES");
         		Cipher cipher=Cipher.getInstance("DES");
        	}catch(Exception e)
        	{
    Then depending on the user selection, inputText is encrypted or encText is decrypted in the switch statement. Both encryption and decryption blocks share the same "keySpec" and "Cipher" variables generated in the above code snippet. However, the compiler says that keySpec and Cipher cannot be resolved which I understand is that the keySpec and Cipher generated cannot be seen by the statements inside the switch blocks. How can I make it work?

    Cheers,

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,431
    Rep Power
    25

    Default Re: How to generate and use variables in different switch statements

    If you want to share variables between different blocks of code(code inside of a pair of {}) then you must define the variable outside of the blocks so that it is in scope for all the code to see and have access to. Move the variables definition outsize of the inner pair of {}s.
    You can define it and give it a null value and then give it the desired value and use it inside of any inner block (within {}s)

  3. #3
    YellowSun is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: How to generate and use variables in different switch statements

    Thanks for your reply, Norm.

    However, the definition is already outside of the switch blocks and wherever I put the definition, it gives the same error as long as the definitions are in "try" statement. If I remove the "try" statement, the definition gives error as the exceptions are not handled then. Is there any other way to solve this?

    Cheers

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to generate and use variables in different switch statements

    Quote Originally Posted by YellowSun View Post
    However, the definition is already outside of the switch blocks and wherever I put the definition, it gives the same error as long as the definitions are in "try" statement. If I remove the "try" statement, the definition gives error as the exceptions are not handled then. Is there any other way to solve this?
    No, there is no other way as Norm is giving you the correct and only way to solve this. He doesn't tell you to remove the try block, just to declare any variables that need to be shared *outside* of try's scope outside of the try block. You then *use* the variables *inside* the try block.

  5. #5
    YellowSun is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: How to generate and use variables in different switch statements

    Thanks it worked!

    Another question: When I try to decode the encrypted text, I obtain something like:

    android.widget.EditText@40f50cd8
    instead obtaining the text I originally put inside the input Text box. What does it mean, what's the error here? It seems like an addressing error?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,431
    Rep Power
    25

    Default Re: How to generate and use variables in different switch statements

    What you see is the classname of the object followed by @ followed by the hashcode for the object you are printing. That is the String returned by the Object class's toString() method. You could override the class's toString method and return a String you'd like to see.
    If it is not your class, read the class's API doc

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to generate and use variables in different switch statements

    It's not an addressing error. What you're getting is the default toString() output of the EditText object. You were trying to print the widget out, not the text that it contains, so you'll need to fix the bug in your code.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to generate and use variables in different switch statements

    Quote Originally Posted by Norm View Post
    What you see is the classname of the object followed by @ followed by the hashcode for the object you are printing. That is the String returned by the Object class's toString() method. You could override the class's toString method and return a String you'd like to see.
    Norm, I don't think that this is coming from a class that he created but rather the EditText android widget. So he shouldn't try to override any toString method here, but rather he needs to extract the widget's text and then print that.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,431
    Rep Power
    25

    Default Re: How to generate and use variables in different switch statements

    I saw that too late and added a further comment.
    Fubarable likes this.

  10. #10
    YellowSun is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: How to generate and use variables in different switch statements

    It worked. Thanks a lot...

Similar Threads

  1. Multiple try & catch statements within a switch
    By Martyn in forum New To Java
    Replies: 21
    Last Post: 01-12-2012, 09:24 PM
  2. switch statements & conditionals
    By linkxs in forum New To Java
    Replies: 7
    Last Post: 09-25-2011, 06:17 AM
  3. switch statements
    By jim01 in forum New To Java
    Replies: 7
    Last Post: 04-10-2011, 10:52 PM
  4. Using a switch/case with my if statements
    By coding in forum New To Java
    Replies: 2
    Last Post: 03-07-2011, 08:01 AM
  5. Replies: 2
    Last Post: 11-22-2008, 05:30 AM

Posting Permissions

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