when i try to use the variable "encryptionKey" outside the loop or if-statement it's declared in, it throws a compile error "cannot find symbol".. any idea?
else if (inputPlainResultArray.length == 4 || inputPlainResultArray.length == 9 || inputPlainResultArray.length == 16)
{
char[] encryptionKey = inputPlainResultArray;
System.out.print("Encryption Key: ");
System.out.print(encryptionKey);
System.out.println();
System.out.println();
System.out.println();
System.exit(0);
}
}
}
Because it's local variable , which means you can't access it outside the scope where it's declared. You should take a look on which types of variables exists in Java
In your particular case you may use instance variable , so you either declare char[] encryptionKey
outside the method:
public class YourClass{
char[] encryptionKey;
// other methods, fields, etc.
}
and you'll be able to use this variable in any place in this class, or declare inside the method, bit outside the else-if
scope:
char[] encryptionKey = null;
if (...){}
else if (...){
char[] encryptionKey = inputPlainResultArray;
}
so it'll be visible for all entities inside this particular method.
That's because the scope of that variable
is in the curly braces of the loop
/ if
statement. You cannot use it like this. Instead declare it outside and use it.
In your case it will look something like this:
char[] encryptionKey = null;
if (...)
...
else if (inputPlainResultArray.length == 4 || inputPlainResultArray.length == 9 || inputPlainResultArray.length == 16)
{
encryptionKey = inputPlainResultArray;
System.out.print("Encryption Key: ");
System.out.print(encryptionKey);
System.out.println();
System.out.println();
System.out.println();
System.exit(0);
}
create the variable outside of the method
char[] encryptionKey;
inside of the method then you can have
encryptionKey = ...
the only problem is if you try to call it before you initialize the variable, so be careful, or take precautions such as if(encryptionKey==null) return;
You cannot access the variable "encryptionKey" from outside the loop because you have declared it inside the loop. Move the declaration outside and it will work.
char[] encryptionKey;
else if (inputPlainResultArray.length == 4 || inputPlainResultArray.length == 9 || inputPlainResultArray.length == 16)
{
encryptionKey = inputPlainResultArray;
....
}
Use break keyword, not System.exit(0);
if(condition){
//do something
break;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.