-1

I'm trying to replace all words (alphabet letters) from JList1 to the number corresponding its place in the alphabet to JList2 with the press of the Run button. (ex. A to 01) And if it's not an English alphabet letter then leaving it as it is. Capitalization doesn't matter (a and A is still 01) and spaces should be kept.

For visual purposes:

https://i.stack.imgur.com/PYhYA.png

"Apple!" should be converted to "0116161205!"
"stack Overflow" to "1920010311 1522051806121523"
"über" to "ü020518"

I have tried a few methods I found on here, but had zero clue how to add the extra 0 in front of the first 9 letters or keep the spaces. Any help is much appreciated.

tags
  • 3
  • 2
  • 1
    Post your code so far. – A J May 09 '18 at 20:16
  • `but had zero clue how to add the extra 0 in front of the first 9 letters` - how do you combine "1" and "16" and "16" and "12" and "5"i nto a single String? Are you using a loop to find each index. If so how hard is to convert the index to a String and determine how many characters are in the String and then add an extra "0" when required. – camickr May 09 '18 at 20:16
  • A simple way would be to just prebuild a `Map` and keep the number as a string, "01" and let it do its thing. – ChiefTwoPencils May 09 '18 at 20:16
  • `A simple way would be...` - yes, but don't the think a beginner should learn the basics of programming (problem solveing) first before using more complex data structures to solve simple problems? I mean are you suggesting to have 26 statements to add the values to the Map. Or do you create a simple loop to populate the Map. I would use a loop, but you still need to know how to take an index and add a "0" at the beginning, when required. – camickr May 09 '18 at 20:32
  • I did try a hashmap before, but at that time i didn't know you can specify yourself which types of data can be entered into one and got an error when trying to set "a" to "01" (was probably set to Character, Integer), but i did learn it now from the answer below. – tags May 09 '18 at 21:18
  • @camickr, you'll have to `@` me if you want me to respond. I suppose I'd say that jumping into a GUI framework has already placed them in a position where they need more complex structures and design knowledge. I learned how to code a hash map my second semester so I don't really see how it's too difficult to *use* one. No, there's no way I'd write the statements, so a loop it is. Seems like a pretty reasonable task in the answer below :). – ChiefTwoPencils May 09 '18 at 21:39

2 Answers2

1

Here is a solution :

//Create a Map of character and equivalent number
Map<Character, String> lettersToNumber = new HashMap<>();
int i = 1;
for(char c = 'a'; c <= 'z'; c++) {
    lettersToNumber.put(c, String.format("%02d", i++));
}

//Loop over the characters of your input and the corresponding number
String result = "";
for(char c : "Apple!".toCharArray()) {
    char x = Character.toLowerCase(c);
    result+= lettersToNumber.containsKey(x) ? lettersToNumber.get(x) : c;
}

Input, Output

Apple!           => 0116161205!
stack Overflow   => 1920010311 1522051806121523
über             => ü020518
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

So given...

(ex. A to 01) And if it's not an English alphabet letter then leaving it as it is. Capitalization doesn't matter (a and A is still 01) and spaces should be kept.

This raises some interesting points:

  • We don't care about non-english characters, so we can dispense with issues around UTF encoding
  • Capitalization doesn't matter
  • Spaces should be kept

The reason these points are interesting to me is it means we're only interested in a small subset of characters (1-26). This immediately screams "ASCII" to me!

This provides an immediate lookup table which doesn't require us to produce anything up front, it's immediately accessible.

A quick look at any ascii table provides us with all the information we need. A-Z is in the range of 65-90 (since we don't care about case, we don't need to worry about the lower case range.

But how does that help us!?

Well, this now means the primary question becomes, "How do we convert a char to an int?", which is amazingly simple! A char can be both a "character" and a "number" at the same time, because of the ASCII encoding support!

So if you were to print out (int)'A', it would print 65! And since all the characters are in order, we just need to subtract 64 from 65 to get 1!

That's basically your entire problem solved right there!

Oh, okay, you need to deal with the edge cases of characters not falling between A-Z, but that's just a simple if statement

A solution based on the above "might" look something like...

public static String convert(String text) {
    int offset = 64;
    StringBuilder sb = new StringBuilder(32);
    for (char c : text.toCharArray()) {
        char input = Character.toUpperCase(c);
        int value = ((int) input) - offset;
        if (value < 1 || value > 25) {
            sb.append(c);
        } else {
            sb.append(String.format("%02d", value));
        }
    }
    return sb.toString();
}

Now, there are a number of ways you might approach this, I've chosen a path based on my understanding of the problem and my experience.

And based on your example input...

String[] test = {"Apple!", "stack Overflow", "über"};
for (String value : test) {
    System.out.println(value + " = " + convert(value));
}

would produce the following output...

Apple! = 0116161205!
stack Overflow = 1920010311 1522051806121523
über = ü020518
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366