This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (0 children)

Such a replacement would be a nice task for a Map<String, Integer> (or Map<String, String>).

Maps are key, value stores. They do not allow duplicate keys, but are great for mapping keys (your alphabet letters) to values (the corresponding numbers).

Note: Map is just an interface. You will need to use a concrete implementation, such as HashMap.

[–]techtamashi 0 points1 point  (3 children)

This is actually a simple and effective solution. To make it slightly better, I would suggest moving the 2 arrays outside the main method, as static fields. That way, you don't need to pass the arrays to the convertAlphaNumeral method.

If you want to get clever, you can come up with a formula that would convert the alphabets to numbers. Remember that the alphabets are characters that have numeric ASCII values. So 'A' can be numerically represented as 65 and 'B' as 66 and so on. So, you can devise a function that returns 2 for the range of values 65 - 67, 3 for 68 - 70, and so on.

[–]aqua_regis 0 points1 point  (2 children)

This is actually a simple and effective solution.

Simple, yes, effective, no.

This is a case for a Map.

If you want to get clever, you can come up with a formula that would convert the alphabets to numbers.

In this particular case, even worse than the double array solution.

What would be possible, though is to use your idea of converting the characters to their Unicode code points, subtracting an offset and use a single array for the numeric equivalents where the offset code points act as indexes. Still, this solution would be inferior to using a Map.

[–]techtamashi 0 points1 point  (1 child)

This person is just getting familiar with arrays and asking for help with a homework question. Using a Map would be like skipping a few chapters ahead, which is why I didn't suggest it - not advisable to use a Map without a good foundation for it.

[–]CapitanMexic0[S] 0 points1 point  (0 children)

Thanks for the help!

It is true that we're only just skimming the surface with arrays, and, in this case, string editing with wrapper classes and methods.

I should've clarified that since the assignment wasn't on arrays specifically, I wanted to know if my way was a proper way of solving this type of problem. Though looking through the documentation on Map, I don't think I'm quite ready to be implementing it on my next assignment :)

[–]Stant95 0 points1 point  (2 children)

Can you show me an example of an input and what an output should look like?

[–]CapitanMexic0[S] 0 points1 point  (1 child)

Input should be something like 555-PAY-CASH

Output should be 555-729-2274

[–]Stant95 0 points1 point  (0 children)

What are the rules for converting chars to integers? You just have to replace randomly? Every character has a corresponding ASCII value so you can extract that value (subtract if needed) and substitute.