programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Beginner Programming Problem- Encryption/Decryption

Brandon Cahrenger
Greenhorn
Posts: 4
Hello, I'm new here but I've been browsing and there is a lot of very excellent information here. I'm also new to programming, having just started this semester. I like it a lot thus far, and although I'm still grasping the basics, I think I have a decent understanding of it all.

Anyways, here is my problem. I've written most of the code (along with another classmate) to get this to work, but I can't seem to return the encrypted value. I'm also trying to figure out how to separate the digits (or if I'm supposed to) to make the numbers encrypted. The main kicker is that we have to be using methods to return the final values. Everything compiles, but any help is very appreciated. Thanks!

Here is the question
A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped. All of their data is transmitted as four-digit integers. They have asked you to write a program that will encrypt their data so that it may be transmitted more securely. Your program should read a four-digit integer number and encrypt it as follows: Replace each digit by the remainder after (the sum of that digit plus 7) is divided by 10. Then, swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer. Do the encryption in a method and send the encrypted number back to main. Here are the method headers: public static int encrypt(int num) // Takes num as a parameter and returns the encrypted value public static int getnum() // gets one number from the user Example: If the user enters 1234 they should see: 0189

Campbell Ritchie
Marshal
Posts: 56536
172
Welcome to the Ranch

I have added code tags to your post. Always use them: doesn't it look better.
You have written a+b+c+d. What is that supposed to do? Will you get a 4‑digit number from that?
Why have you marked all your methods static?

Campbell Ritchie
Marshal
Posts: 56536
172
You also have repeated code with adding 7 and taking the remainder. That should be separated into a method of its own.

Brandon Cahrenger
Greenhorn
Posts: 4
Thank you for the response Campbell Ritchie.

Thanks for the Code Tags, it does look much better.

The a+b+c+d is probably an error on my part (or it is an error on my part). That's definitely part of the problem and I'm trying to figure it out.

As for the static, that's what I've been taught so far, so that's all I know (although I'm researching it all as the info comes to me).

Campbell Ritchie
Marshal
Posts: 56536
172
What about 1000 * a + 100 * b + 10 * c + d?

Shiv Tattva
Greenhorn
Posts: 13
Try this

Tyson Lindner
Ranch Hand
Posts: 211
Basically you have to add the digits together as strings (not ints) to get a string, then convert your string back to an int before returning. It might be useful to use the Integer class from the start in order to do this.