0

Actually I'm sorry for the incomplete code I guess maybe this is better.. What I want is after the algorithm is done I want to be able to print out the number output and convert the number output into letters.. Most likely just like cipher

public static void main(String args[])
{
    Scanner scanner = new Scanner(System.in);
    String ct;
    String k;
    int sum = 0;

    System.out.print("Enter Text: ");
    ct = scanner.nextLine();
    int arr1[] = new int[ct.length()];

    for(int j = 0; j < ct.length(); j++)
    {
        arr1[j] += ( (int)ct.charAt(j) - 97);
        System.out.println("Value of Text at["+j+"]: "+arr1[j]);
    }


    //user input key
    System.out.print("Enter random word: ");
    k = scanner.nextLine();

    for(int i = 0; i < k.length(); i++)
    {
        System.out.println( (int)k.charAt(i) - 97 );
        sum = sum + ( +(int)k.charAt(i) - 97 );
    }
    System.out.println("Sum of all letter in the random word: "+sum);
    for(int i = 0; i < ct.length(); i++)
    {
        System.out.print(arr1[i]+ " ");
        arr1[i] += sum;
        if(arr1[i] > 25)
        {
            arr1[i] -= 25;
            if(arr1[i] > 25)
            {
                arr1[i] -= 25;
            }
        }
    }
    System.out.println();
    for(int i = 0; i < ct.length(); i++)
    {
        System.out.print(arr1[i]+ " ");
    }

}

Output

Enter Text: kind
Value of Text at[0]: 10
Value of Text at[1]: 8
Value of Text at[2]: 13
Value of Text at[3]: 3
Enter random word: evil
4
21
8
11
Sum of all letter in the random word: 44
10 8 13 3 
4 2 7 22 
leee kebin
  • 1
  • 1
  • 2
  • You want to *invert* the code / logic? Well, what is the inversion of `... + " "`? That is basically `String.valueOf(...)`, try `Integer.valueOf(...)` for the inverse, I think you can figure out the opposite of `... - 97` on you own. – luk2302 Nov 09 '18 at 17:43
  • 6
    Possible duplicate of [How do I convert a number to a letter in Java?](https://stackoverflow.com/questions/10813154/how-do-i-convert-a-number-to-a-letter-in-java) – Eamon Scullion Nov 09 '18 at 17:46

3 Answers3

0

You could use an array of characters char[] letters = "ABCD".toCharArray();

The letter you want is at the location of your number in the array.

System.out.println(letters[0]);

Would print ‘A’

This only works because you want A to start at 0 instead of 97.

0

You can create a method:

public String getCharacterFromInt(int i) {
    return String.valueOf((char)(i + 'A'));
}

And you can call the method:

System.out.println("Value: "+getCharacterFromInt(0));

With 0, return A and so on.

gonzaloan
  • 371
  • 2
  • 13
0

let me know if you are trying to do something like this .

public class MainClass {
    public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    String s=scanner.nextLine();
    for(int i=0;i<s.length();i++){
        System.out.println("Value of the text at ["+i+"]: "+((int) s.charAt(i)-22)%25);
    }

  }
}

if the input is:evil

Output will be:

Value of the text at [0]: 4

Value of the text at [1]: 21

Value of the text at [2]: 8

Value of the text at [3]: 11

Abdul Momen
  • 379
  • 2
  • 13
  • yeah something like that, but how do you do the inverse? how can you convert it back to letters? – leee kebin Nov 11 '18 at 16:11
  • To do so you will be needing the division result( (int) s.charAt(i)-22)/25 ) , so you can store it somewhere using a map may be suitable for it. then it is easy to get back the char as for 25*3+4+22=101 which is `e` (here 3 is division result)....i think it helps now...if it works please accept my answer. – Abdul Momen Nov 11 '18 at 17:15