+ 2

whats the problem?

this code is an encryption code. i tried to do the encryption using methods and i have an error that i dont know how to fix. code: https://code.sololearn.com/cMMXY723qqoW whats the problem? how to fix it?

11th Jul 2020, 11:23 AM
Yahel
Yahel - avatar
9 Réponses
+ 2
i doesn't exist in the decrypt() method. You're not saving the encrypted text at all. Just outputting each char. Try saving the encrypted text and returning it from the call to e.encrypt() then pass the encrypted text to the decrypt method. e.decrypt(txt)
11th Jul 2020, 11:48 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
BTW, don't ever use this type of "encryption" for an actual program. It's fine for practicing and learning, but if this where used to say encrypt actual passwords etc, it would be far too easy to crack. https://code.sololearn.com/c2AQOv5V13GI/?ref=app
11th Jul 2020, 12:05 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
yahel here's a way just reusing the sentence variable of the class. https://code.sololearn.com/cOM2ka68pOZn/?ref=app
11th Jul 2020, 12:36 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
You don't need it in those locations. I was being explicit for clarity. I've removed it so you can see that the code works the same. 'this' just refers to the current instance of the object.
11th Jul 2020, 12:45 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thanks!
11th Jul 2020, 12:48 PM
Yahel
Yahel - avatar
0
i am trying and i cant find a valid way to make it work? can you show me how you do it?
11th Jul 2020, 11:55 AM
Yahel
Yahel - avatar
0
Ok, is there an other way that I just store a variable outside the method and then just assigning the encrypted sentence to it?
11th Jul 2020, 12:10 PM
Yahel
Yahel - avatar
0
public static void main(String[] args) { ... e.encryped(); System.out.println(); e.setSentence("nkrru2&s&tgsk&oy&gnkr''"); e.decryped(); public void decryped(){ for (char d : sentence.toCharArray() ){
11th Jul 2020, 12:27 PM
zemiak
0
Why do you need the 'this' keyword in line 39 btw?
11th Jul 2020, 12:39 PM
Yahel
Yahel - avatar