In: Computer Science
I can't figure out why this won't run.
//CryptographyTest.java
//package cryptography;
import java.util.Scanner;
public class CryptographyTest {
public static void main(String[] args) {
// create a scanner object to read from user
Scanner s = new Scanner(System.in);
// prompt user for input option
while(true) {
// loop till user say exit
System.out.println("Select Option:");
System.out.println("1. Encrypt");
System.out.println("2. Decrypt");
System.out.println("3. Exit");
String input = s.nextLine(); // get user input
int option = 0;
try {
// check for valid input
option = Integer.parseInt(input);
if(option < 1 || option > 3) {
throw new IllegalArgumentException();
}
} catch (Exception e) {
// print error message
System.out.println("Invalid Input!");
}
// check for user option
if(option == 1) {
// ask user for data to encrypt
System.out.print("Enter 4 digit number to Encrypt: ");
int data = getInput(s); // get user input
// check for valid input
if(data == -1) {
// print error message
System.out.println("Invalid Input!");
}
else {
System.out.print("Encrypted number is: ");
String encrypt_number = "" + Cryptography.encrypt(data);
// add zeroes at start of number if needed
int len = encrypt_number.length();
for(int i=4;i>len;i--) {
encrypt_number = "0" + encrypt_number;
}
System.out.println(encrypt_number);
}
}
else if(option == 2) {
// ask user for data to decrypt
System.out.print("Enter 4 digit number to Decrypt: ");
int data = getInput(s); // get user input
// check for valid input
if(data == -1) {
// print error message
System.out.println("Invalid Input!");
}
else {
System.out.print("Decrypted number is: ");
String decrypt_number = "" + Cryptography.decrypt(data);
// add zeroes at start of number if needed
int len = decrypt_number.length();
for(int i=4;i>len;i--) {
decrypt_number = "0" + decrypt_number;
}
System.out.println(decrypt_number);
}
}
else if(option == 3) {
// break the loop and exit program
System.out.println("Bye!");
break;
}
}
}
private static int getInput(Scanner s) {
String input;
// get user input
input = s.nextLine();
// check for input length
if(input.length() != 4) {
// return as input is not valid
return -1;
}
else {
// check for integer input
try {
int data = Integer.parseInt(input);
// return the input in integer form
return data;
} catch (Exception e) {
// return as input is not integer type
return -1;
}
}
}
}
(goes with this:)
//Cryptography.java
//package cryptography;
public class Cryptography {
public static int encrypt(int input) {
// input is a 4 digit integer
// convert integer to string
String str = "" + input;
// add zeroes at start of number if needed
int len = str.length();
for(int i=4;i>len;i--) {
str = "0" + str;
}
// get individual digits
int first_digit = str.charAt(0) - '0'; // remove '0' to convert
char to digit
int second_digit = str.charAt(1) - '0';
int third_digit = str.charAt(2) - '0';
int fourth_digit = str.charAt(3) - '0';
// add 7 to each digit and take reminder by dividing 10
first_digit = (first_digit + 7) % 10;
second_digit = (second_digit + 7) % 10;
third_digit = (third_digit + 7) % 10;
fourth_digit = (fourth_digit + 7) % 10;
// replace first digit with third and second with fourth
String encrypt_number = "" + third_digit + fourth_digit +
first_digit + second_digit;
// convert string to integer and return it
return Integer.parseInt(encrypt_number);
}
public static int decrypt(int input) {
// input is 4 digit integer
// convert integer to string
String str = "" + input;
// add zeroes at start of number if needed
int len = str.length();
for(int i=4;i>len;i--) {
str = "0" + str;
}
// get individual digits
int first_digit = str.charAt(0) - '0'; // remove '0' to convert
char to digit
int second_digit = str.charAt(1) - '0';
int third_digit = str.charAt(2) - '0';
int fourth_digit = str.charAt(3) - '0';
// add 3 to each digit and take reminder by dividing 10
first_digit = (first_digit + 3) % 10;
second_digit = (second_digit + 3) % 10;
third_digit = (third_digit + 3) % 10;
fourth_digit = (fourth_digit + 3) % 10;
// replace first digit with third and second with fourth
String encrypt_number = "" + third_digit + fourth_digit +
first_digit + second_digit;
// convert string to integer and return it
return Integer.parseInt(encrypt_number);
}
}
Please find your solutino below the code is corrected if any doubt comment and do upvote.
CryptographyTest.java:
import java.util.Scanner;
public class CryptographyTest {
private static int getInput(Scanner s) {
String input;
// get user input
input = s.nextLine();
// check for input length
if(input.length() != 4||Integer.parseInt(input)<0) {
// return as input is not valid
return -1;
}
else {
// check for integer input
try {
int data = Integer.parseInt(input);
// return the input in integer form
return data;
} catch (Exception e) {
// return as input is not integer type
return -1;
}
}
}
public static void main(String[] args) {
// create a scanner object to read from user
Scanner s = new Scanner(System.in);
// prompt user for input option
while(true) {
// loop till user say exit
System.out.println("Select Option:");
System.out.println("1. Encrypt");
System.out.println("2. Decrypt");
System.out.println("3. Exit");
String input = s.nextLine(); // get user input
int option = 0;
try {
// check for valid input
option = Integer.parseInt(input);
if(option < 1 || option > 3) {
throw new IllegalArgumentException();
}
}
catch (Exception e) {
// print error message
System.out.println("Invalid Input!");
}
// check for user option
if(option == 1) {
// ask user for data to encrypt
System.out.print("Enter 4 digit number to Encrypt: ");
int data = getInput(s); // get user input
// check for valid input
if(data == -1) {
// print error message
System.out.println("Invalid Input!");
}
else {
System.out.print("Encrypted number is: ");
String encrypt_number = "" + Cryptography.encrypt(data);
// add zeroes at start of number if needed
int len = encrypt_number.length();
for(int i=4;i>len;i--) {
encrypt_number = "0" + encrypt_number;
}
System.out.println(encrypt_number);
}
}
else if(option == 2) {
// ask user for data to decrypt
System.out.print("Enter 4 digit number to Decrypt: ");
int data = getInput(s); // get user input
// check for valid input
if(data == -1) {
// print error message
System.out.println("Invalid Input!");
}
else {
System.out.print("Decrypted number is: ");
String decrypt_number = "" + Cryptography.decrypt(data);
// add zeroes at start of number if needed
int len = decrypt_number.length();
for(int i=4;i>len;i--) {
decrypt_number = "0" + decrypt_number;
}
System.out.println(decrypt_number);
}
}
else if(option == 3) {
// break the loop and exit program
System.out.println("Bye!");
break;
}
}
}
}
Cryptography.java:
public class Cryptography {
public static int decrypt(int input) {
// input is 4 digit integer
// convert integer to string
String str = "" + input;
// add zeroes at start of number if needed
int len = str.length();
for(int i=4;i>len;i--) {
str = "0" + str;
}
// get individual digits
int first_digit = str.charAt(0) - '0'; // remove '0' to convert char to digit
int second_digit = str.charAt(1) - '0';
int third_digit = str.charAt(2) - '0';
int fourth_digit = str.charAt(3) - '0';
// add 3 to each digit and take reminder by dividing 10
first_digit = (first_digit + 3) % 10;
second_digit = (second_digit + 3) % 10;
third_digit = (third_digit + 3) % 10;
fourth_digit = (fourth_digit + 3) % 10;
// replace first digit with third and second with fourth
String encrypt_number = "" + third_digit + fourth_digit + first_digit + second_digit;
// convert string to integer and return it
return Integer.parseInt(encrypt_number);
}
public static int encrypt(int input) {
// input is a 4 digit integer
// convert integer to string
String str = "" + input;
// add zeroes at start of number if needed
int len = str.length();
for(int i=4;i>len;i--) {
str = "0" + str;
}
// get individual digits
int first_digit = str.charAt(0) - '0';
// remove '0' to convert char to digit
int second_digit = str.charAt(1) - '0';
int third_digit = str.charAt(2) - '0';
int fourth_digit = str.charAt(3) - '0';
// add 7 to each digit and take reminder by dividing 10
first_digit = (first_digit + 7) % 10;
second_digit = (second_digit + 7) % 10;
third_digit = (third_digit + 7) % 10;
fourth_digit = (fourth_digit + 7) % 10;
// replace first digit with third and second with fourth
String encrypt_number = "" + third_digit + fourth_digit + first_digit + second_digit;
// convert string to integer and return it
return Integer.parseInt(encrypt_number);
}
}