0

I'm currently working on a project, and I don't understand why my second method doesn't work. I'm attempting to change the numbers entered by the user to another number. Each number should be added +7, then divided by 10. The first and third digit are supposed to be swapped, as are the second and fourth. It is supposed to accept four digits, and work with each one individually. I'm not sure how to get the results my professor is asking for. He said the result of 1234 should be 0189, but instead I keep getting 13 no matter how I swap them... I have attempted to modify the method to separate the digits, then perform the math required afterwards, but now the result is 14... I also don't understand how to have the compiler return a zero at the beginning of the number, or to keep it from dropping the zero at the beginning of the number when it is returned. Any help would be appreciated, but please don't hate on me for not understanding this assignment.

import java.util.*;

public class SeayJ_program2
{
  static Scanner input = new Scanner (System.in);
  public static void main (String [] args)
  {


  int num = getnum(); 

  System.out.println(encrypt(num));

  }
  // methods are here  
  public static int getnum()
  {
  int num;

     System.out.println("Please enter a four digit number");
     num = input.nextInt();

  return num;    
  }


  public static int encrypt(int num)
  {
    int digit1, digit2, digit3, digit4;
    digit4 = num % 10;
    num = num / 10;
    digit4 = (num + 7) / 10;
    digit3 = num % 10;
    num = num / 10;
    digit3 = (num + 7) / 10;
    digit2 = num % 10;
    num = num / 10;
    digit2 = (num + 7) / 10;
    digit1 = num % 10;
    num = num / 10;
    digit1 = (num + 7) / 10;
    return digit3 + digit4 + digit1 + digit2;
  }

  }  
8
  • 2
    Why in the hell is he asking you to add 7 before dividing by 10? That serves no useful purpose at all. Commented Apr 21, 2014 at 21:55
  • The leading zeros are only present in the ASCII representation of the integer value. Please understand the difference between the two. Commented Apr 21, 2014 at 21:56
  • Anyway, try reversing the order of the digits. Commented Apr 21, 2014 at 21:56
  • Each number in the four digit sequence must be separated, increased by seven, then divided by ten. He said that is how it is supposed to be encrypted. Commented Apr 21, 2014 at 21:57
  • I think you are misunderstanding the problem or something because how are you going from 1234 and getting 0819? I cannot see the math here. Commented Apr 21, 2014 at 22:00

2 Answers 2

2

This should work for getting the encrypted number. Think how to handle the missing zero(s)

public static int encrypt(int number){
    int temp = number;
    int digit4 = (temp + 7) % 10;
    temp = temp / 10;
    int digit3 = (temp + 7) % 10;
    temp = temp / 10;
    int digit2 = (temp + 7) % 10;
    temp = temp / 10;
    int digit1 = (temp + 7) % 10;
    return (digit3 * 1000 + digit4 * 100 + digit1 * 10 + digit2);

}

Sign up to request clarification or add additional context in comments.

1 Comment

Okay, I understand the needing to multiply the numbers that have to be moved by their respective places that they will occupy. This keeps me from having to separate the numbers using division and modulus. Now I have to figure out how to read in this integer as a string so I can get my zeroes to appear. Thank you
0

You would use Jasypt in this you can invoke mask and unmask

1 Comment

I don't think I'm supposed to deviate too far from the instructions. He wants it to use the methods above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.