-2

If I sub out 'a' and 'b' for 2 and 8 i get 95 which is the answer i want. But it keeps returning 73 if i give the input 28

class test2a {
    public static void main(String[] args) {
      Scanner date = new Scanner(System.in);  // Create a Scanner object
      System.out.println("2 digits of date");
      int edate = date.nextInt();  // Read user input

      int a =(String.valueOf(edate).charAt(0));
      int b =(String.valueOf(edate).charAt(1));

      int digita = (7+a) % 10;
      int digitb = (7+b) % 10;

      String result = "" + digita + digitb;
      System.out.println("Ecryption: " + result);  // Output user input
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • 1
    Did you mean to tag this with 'javascript'? Java and javascript are two different programming languages – lucasvw Jul 06 '20 at 18:54
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Andy Turner Jul 06 '20 at 18:56
  • 1
    "but it keeps returning 73 if I give the input 28." What do you expect it to do instead? – Andy Turner Jul 06 '20 at 18:58
  • 1
    String.charAt(i) returns the ascii code not the number itself. e.g. the ascii code for '0' is 48 – David Zimmerman Jul 06 '20 at 18:58
  • `java !=== javascript` – KunLun Jul 06 '20 at 18:58
  • the charAt gives characters '2' and '8' which when converted to int gives you their ascii values 50 and 56 so you digita is 57%10 = 7 and digitb is 63%10 = 3 which results into your 73. Simplest change would be to make use of Integer.valueOf on the character returned. – Vivek Jul 06 '20 at 19:00
  • @Vivek Good explanation; but `Integer.valueOf('2')` still gives 50. – Ole V.V. Jul 06 '20 at 19:05
  • @OleV.V. thanks for pointing this out, indeed it is not the same as Integer.valueOf("2") – Vivek Jul 06 '20 at 19:14
  • Thanks everyone for the help Im only 2 weeks into this field so I apologise for the lack of knowledge – Gorden Bob Ang Jul 06 '20 at 20:00
  • FYI, `char` type is obsolete. Learn to use Unicode [code point](https://en.wikipedia.org/wiki/Code_point) integer numbers instead. – Basil Bourque Jul 06 '20 at 20:10

1 Answers1

0

You need to get the numerical value of the character i.e.

Character.getNumericValue

Unless you need integer input validation, you can reduce these two lines:

int edate = date.nextInt();  // Read user input
char[] digits = new Integer(edate).toString().toCharArray();

To this:

char[] digits = date.nextLine().toCharArray(); // Read user input

Example

public class EncryptDigits {
    public static void main(String[] args) {
        Scanner date = new Scanner(System.in);  // Create a Scanner object
        System.out.println("2 digits of date");

        int edate = date.nextInt();  // Read user input
        char[] digits = new Integer(edate).toString().toCharArray();

        int a = Character.getNumericValue(digits[0]);
        int b = Character.getNumericValue(digits[1]);

        int digita = (7+a) % 10;
        int digitb = (7+b) % 10;

        String result = String.format("%d%d", digita, digitb);

        System.out.println("Ecryption: " + result);  // Output user input
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132