Issue
I am a freshman in a CS degree and learning Java. Would you mind bearing with me as I try to explain my problem as I'm still new to Java/IDE and still not too familiar with how everything works together?
To start, my professor has given us a simple assignment to encrypt/decrypt four-digit values when a user is asked. (For, e.g. "Enter four digits integer to be encrypted" - 9835)
The code will then use some formulas (not exactly proper encryption), but it will turn the four digits into a new four-digit number. So for this example, 9835 encrypted will be 0265.
So when using Netbeans as the preferred IDE by my course, the code works well, and the answer for 9835 is 0265.
But today, when I decided to try IntelliJ Idea Community Edition and copied the exact same code, the answer is supposed to be 0265, but the output only shows 265. Somehow it doesn't show 0 as the first digit.
Please note, I have considered converting the encrypted variable from an integer to a string - so 0 will be displayed, but my prof says that it can be done without any converting (when using Netbeans).
What can I do so that when running code in IntelliJ shows the exact output as Netbeans? (Answer is 0265)
Thank you so much, and any help/advice is greatly appreciated.
Below is my code for your reference. (It may be confusing but it works.)
import java.util.Scanner;
class Encryption_Decryption
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//Variables to store the inputs
int encrypted_value_input , decrypted_value_input;
//Variables for storing the individual digits.
int digit1_E , digit2_E , digit3_E , digit4_E; //Where digit1_E is the extreme left position and _E stands for Encrypted.
//Variables for storing the digits with the formula applied.
int a1_e , a2_e , a3_e , a4_e;
//Variables for storing the encrypted and decrypted values.
int encrypted_value , decrypted_value;
//Welcome Message
System.out.println("This is an Encryption and Decryption Application.");
System.out.println();
//Start of Encryption
System.out.print("Enter 4 digits to be Encrypted: ");
encrypted_value_input = input.nextInt();
digit1_E = (encrypted_value_input / 1000); //takes the first digit
digit2_E = ((encrypted_value_input / 100) % 10);
digit3_E = ((encrypted_value_input / 10) % 10);
digit4_E = (encrypted_value_input % 10);
//Encryption Formula
a1_e = (digit1_E + 7) % 10;
a2_e = (digit2_E + 7) % 10;
a3_e = (digit3_E + 7) % 10;
a4_e = (digit4_E + 7) % 10;
//Swapping the digits. For eg. 1234 is abcd. abcd needs to be swapped to become cdab.
int temp1 = a1_e;
a1_e = a3_e;
a3_e = temp1;
temp1 = a2_e;
a2_e = a4_e;
a4_e = temp1;
encrypted_value = a1_e * 1000 + a2_e * 100 + a3_e * 10 + a4_e;
System.out.println("The encrypted value is " + encrypted_value);
}
}
Solution
Giving 9835 as an input,
In the line:
encrypted_value = a1_e * 1000 + a2_e * 100 + a3_e * 10 + a4_e;
you will see that a1e * 1000
is 0, a2e * 100
is 2, a3e * 10
is 6 and a4e
is 5.
In mathematics, this would be equal to 0 + 200 + 60 + 5. Hence, the program returns the value 265. Since you require '0' at the beginning of the encrypted value, the optimal way to solve this is by putting these values into a String.
You will also find that in the lines:
int temp1 = a1e;
a1e = a3e;
a3e = temp1;
temp1 = a2e;
a2e = a4e;
a4e = temp1;
a1e = 0
, a2e = 2
, a3e = 6
, a4e = 5
- so you've already got your answer here.
Now all that's left for you to do is to print these values in a String - in such a way that you have the original integers (for decrypting later on) but also print out the correct answer.
Here's how you can do it:
int concA1E = a1e;
int concA2E = a2e;
int concA3E = a3e;
int concA4E = a4e;
System.out.println("The encrypted value is " + concA1E+concA2E+concA3E+concA4E);
By not adding any spaces between the '+' symbols and the integer variables, you can make them concatenate, and this would result in the answer 0265.
You might as well use the original a1e
,a2e
,a3e
and a4e
integers for concatenation, but I would prefer storing them in separate variables, just in case.
I would advise you to learn how to debug in the IDE of your choice, since you'll be able to see what's being stored in your variables, and the things that Java is carrying out. That's the way I found the solution for this question.
As you can see in the image below, this program works.
Answered By - Chaitanya S
Answer Checked By - Mildred Charles (JavaFixing Admin)