Please, help .. return not working. string can not be resolved to a variabl

Greetings..
I am writing a Java code that encrypt a String using DES. After encryption, I want to return the String, but I get error that says: variable can not be resolved. Please, help me to find out what is the problem ?? Thanks in advance.
Here is the code:
import javax.crypto.*;
import java.security.*;
public class Client {
     public String DESEncrypt(String cardnum)
          try{
          //create cipher object for DES
          Cipher c=Cipher.getInstance("DES");
          // craete the symmetric key for DES
          KeyGenerator kg=KeyGenerator.getInstance("DES");
          Key key=kg.generateKey();
          // DO the ecryption steps then display the result
          c.init(Cipher.ENCRYPT_MODE, key);
          //Dofinal accepts byts only. Thats why we use getbytes method.
          byte[] cardNoBytes=cardnum.getBytes("UTF8");
          //get the encrypted bytes
          byte[] encryptedCardNoBytes=c.doFinal(cardNoBytes);
          //convert bytes to string
          String cardNoString=new String(encryptedCardNoBytes);
          } catch(Exception e){ System.out.print("Error");
          return cardNoString;
     } //end the DESEncrypt method
public static void main (String[] args) {
try{
     Client cl=new Client();
     String x2=cl.DESEncrypt("123");
     System.out.println("The Encrypted text using DES is: " + x2+'\n');
} catch(Exception e){ System.out.print("Error");
}//end main
}

Next time, paste in the exact, complete error message and indicate clearly exactly which line is causing it.
Your problem is that you declare cardNoString inside the try block, but you try to return it outside. Local variables only exist in the narrowest block in which they're declared. Once you exit try, that variable does not exist.
You also have a logical problem. If an error occurs, you're just printing out :"Error", but you're not actually handling it. You're going on as if everything is fine. Catching an exception doesn't fix the error. If you can't fix it, don't catch it.
What do you think your method will return if an exception occurs before you set cardNoString? What do you think it will do to the caller to have some arbitrary, meaningless value returned when he thinks everything went fine and he got a valid result?

Similar Messages

Maybe you are looking for

  • I get a blank white screen when I try to play games on Club Pogo

    as stated earlier, when I go to club pogo, all I get is a blank white screen, firefox will go to site but will not load the games,

  • Satellite P200D - Value Added Package error on Windows 7 64bit

    Satellite P200D-12F (PSPBQE) and Windows 7 64bit TVAP ERROR! I cant find any solution to this problem on the whole internet. can someone please contact Toshiba providers about this because this is really silly? Noone wanna answer me! PLZ! help

  • Corrupted image files

    I create files using PS.  Sometimes, these files will fail to open in PS at a later date.  The error msg is that the file is corrupted.  I can open them in Illustrator to view them.  But they are lost to opening in PS.  Why? And what can I do to corr

  • Custom Step Type: Sequence Call

    OK, I'm primarily a LabVIEW programmer, but in TestStand I understand how to: * Create a Custom Step Type * Link a LabVIEW Module for execution, post substep, edit substep, etc. * Prevent the user from selecting a different module for an instance of

  • What does hardware model iPad 3,3 mean?

    My iPad is a 3rd generation GSM model. But the diagnostic data sent by my iPad to the apple server(it's a built in feature) states that my hardware model Is iPad 3,3. So far I know for CDMA model they use 3,3 but for GSM it should be 3,2. Am I wrong?