0

I am trying to make somewhat of a encoder thing. You enter a code with 1 letter ranging from A to C and four numbers ranging from 1 to 9 (EX: A3256). Here's where it gets tricky. I need to split this up into an Array or whatever is best (EX: [1]A [2]3 [3]2 [4]5 [5]6), so that later I can use each letter/number by itself. Please help!

user2406223
  • 39
  • 2
  • 7

1 Answers1

0

Consider this

    int[] array = new int[5];

    char c = 'C';
    array[0] = c;

    if (array[0] > 9) {
        System.out.println((char) array[0]);
    } else {
        System.out.println(array[0]);
    }

I'm using an int array, which can store chars converted to int. Then I check is the value is above 9, if it is, I cant it back to a char.

Since all letter characters' decimal values are greater than 9, the conversion back to char will occur with the (char) cast.

For example 'C' will get stored in the array as 67 ('C's ascii decimal value). Then it gets converted back to 'C'

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I see what you mean, but I need something more along the lines of using a Scanner to get the input and then splitting that into the chars and separate numbers (EX: Instead of [1]A [2]3256 I need [1]A [2]3 [3]2 [4]5 [5]6). – user2406223 Nov 22 '13 at 01:01
  • I don't see why you can't use a scanner. The above is just an example of how the numbers and letters can coexist in the same array. then access them by the index. If the value is greater than 9, you will access the `char` value. `array[0] = 65 (A); array[1] = 3; array[2] = 3 and so on`. You really need to show some code ,or else your question is simply to vague to answer – Paul Samsotha Nov 22 '13 at 01:23