0

I'm trying to learn Java but I'm stuck at an exercise: I have to write a "decrypt()" method, that converts a String like b.aab and returns to double value 1.001.

I'm also given a 2 dimensional array of characters and the number they represent.

private static String conversionTable[][] = {
        {"a", "0"},
        {"b", "1"},
        {"c", "2"},
        {"d", "3"},
        {"e", "4"},
        {"f", "5"},
        {"g", "6"},
        {"h", "7"},
        {"i", "8"},
        {"j", "9"},
};

I know there is technique to convert a char ch into a digit with its ascii value, something like ch -'a' + 1, but I'm not sure how to apply this here. So my question is: How can I convert a String of letters to String of corresponding digits? From there I would use parseDouble(); to return a double of the String. Thank you in before.

  • You can probably use `String.replace()`. The way you've suggested also works, but because Strings are immutable, I feel like it might be somewhat extra work to pull off. – Compass Nov 05 '14 at 15:13
  • 1
    Welcome to StackOverflow ;) Have you tried anything yet? Where are you stuck actually? – sp00m Nov 05 '14 at 15:15
  • Use ascii value to do this ! – StackFlowed Nov 05 '14 at 15:17
  • If you go the ascii subtraction route, make sure you put in a check for the decimal point, and don't "convert" it. – azurefrog Nov 05 '14 at 15:29

3 Answers3

0

You should try like this

char ch='A';
int value=(int)ch-17;
System.out.println(value);
Adrian Totolici
  • 223
  • 2
  • 20
0
import java.util.Scanner;

public class Program10 {

         public static void main(String[] args) {

                System.out.println("Enter a string to decrypt");
                Scanner sc = new Scanner(System.in);
                String input = sc.nextLine();

              decrypt(input);
    }

      public static void decrypt(String input) {

          char ch;
          String str = "";
          int length = input.length();

         for(int i = 0; i < length ; i++) {

                ch = input.charAt(i);

                   if(ch != '.') {  
                     int val = (int) ch;
                    str = str + (val - 97); 
                  } else {

                     String x = "" + ch;
                     str = str.concat(x);
                }

         }
         System.out.println(str);
}

}

Aamir
  • 2,380
  • 3
  • 24
  • 54
-1

This does the trick using Java8:

public static void main(String args[]){
    String t="abcd";
    for(String s:t.split("")){
        System.out.println(s.charAt(0)-'a');
    }
}

If it is all lowercase, you could make use of the fact, that 'a'-'a'=0. Using Java4-7 you have to skip the empty char.

Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
  • 1
    You should run your code before posting it. Your `split()` call creates an array of `["", "a", "b", "c", "d"]`, so when you loop and try to `charAt(0)` on `""` you're going to get a `StringIndexOutOfBoundsException`. – azurefrog Nov 05 '14 at 15:27
  • I used Java8. Does that matter? I tested the code on my machine: http://imgur.com/Bi7OrMS – Thomas Junk Nov 05 '14 at 15:30
  • Interestingly enough there is. The way that the `split()` method is defined has changed. See [this question](http://stackoverflow.com/questions/22718744/why-does-split-in-java-8-sometimes-remove-empty-strings-at-start-of-result-array) for a complete explanation. I guess from now on, `split()`-based questions and answers should be tagged with the Java version to be safe. – azurefrog Nov 05 '14 at 19:14
  • You should probably make a note in your answer that it won't work for Java 7 or lower, or perhaps write one solution for Java 8 and one for Java 4-7. – azurefrog Nov 05 '14 at 19:19