Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Ok im going to try and explain my problem here and what I need to do is convert a string array into an int array.

Here is part of what I have (well the initial set up)

   System.out.println("Please enter a 4 digit number to be converted to decimal ");
    basenumber = input.next();

    temp = basenumber.split("");
    for(int i = 0; i < temp.length; i++)
        System.out.println(temp[i]);

    //int[] numValue = new int[temp.length];
    ArrayList<Integer>numValue = new ArrayList<Integer>();

    for(int i = 0; i < temp.length; i++)
        if (temp[i].equals('0')) 
            numValue.add(0);
        else if (temp[i].equals('1')) 
            numValue.add(1);
                     ........
        else if (temp[i].equals('a') || temp[i].equals('A'))
            numValue.add(10);
                     .........
             for(int i = 0; i < numValue.size(); i++)
        System.out.print(numValue.get(i));

Basically what I am trying to do it set 0-9 as the actual numbers and then proceed to have a-z as 10-35 from the input string such as Z3A7 ideally would print as 35 3 10 7

share|improve this question
1  
What is the question? – Subhrajyoti Majumder Dec 5 '12 at 7:21
so what is the question? you want to optimize the code or you are getting some problems? – Bhavik Shah Dec 5 '12 at 7:25
The issue is that code I posted accepts the string and breaks it down into an array but that is all it does. It wont add values to a new array or change values. – mad hatter Dec 5 '12 at 8:05

3 Answers

up vote 2 down vote accepted

You can use this single line in the loop (assuming user doesn't enter empty string):

int x = Character.isDigit(temp[i].charAt(0)) ?
        Integer.parseInt(temp[i]) : ((int) temp[i].toLowerCase().charAt(0)-87) ;

numValue.add( x );

Explanation of the code above:

  • temp[i].toLowerCase() => z and Z will convert to the same value.
  • (int) temp[i].toLowerCase().charAt(0) => ASCII code of character.
  • -87 => Substracting 87 for your specification.
share|improve this answer
...that doesn't handle numbers, does it? – Jeff Dec 5 '12 at 7:29
That looks good, but I think you actually want 97 - "a" - not 87 - "W"... – Jeff Dec 5 '12 at 7:45
@Jeff Ascii for "Z" is 122 so we should subtract 87 to get 35, op specifies that. – Juvanis Dec 5 '12 at 7:47
Yeah, I just realised that and was changin my comment when you posted yours. My bad... – Jeff Dec 5 '12 at 7:48
I was trying this out me and a friend had discussed the ascii, but neither of us knew what to do about it. After I emplemented the code it threw an exception java.lang.StringIndexOutOfBoundersException: String index out of range: 0 @ java.lang.StringcharAt(Unknown Source) @ base.mainjava:38 and that is the first line of your code was there something I needed to import? – mad hatter Dec 5 '12 at 8:01
show 4 more comments

Try this in your loop:

Integer.parseInt(letter, 36);

This will interpret letter as a base36 number (0-9 + 26 letters).

Integer.parseInt("2", 36); // 2
Integer.parseInt("B", 36); // 11
Integer.parseInt("z", 36); // 35
share|improve this answer
Probably the easiest and most concise answer here. – Makoto Dec 5 '12 at 7:46
That's very cool, and I'm sure it should have been obvious to me... – Jeff Dec 5 '12 at 7:46
would this result in an Integer.parseInt(""); for every line that needed? If so that is fine I'm just curious and checking all the possible answers. – mad hatter Dec 5 '12 at 8:10
@madhatter, you would need to pass each letter through Integer.parseInt – RobEarl Dec 5 '12 at 9:38

Considering that you want to denote Z as 35, I have written the following function

UPDATE :

The ASCII value for Z is 90, so if you want to denote Z as 35 then you should subtract every character from 55 as (90-35=55):

public static int[] convertStringArraytoIntArray(String[] sarray) throws Exception {
    if (sarray != null) {
        int intarray[] = new int[sarray.length];
        for (int i = 0; i < sarray.length; i++) {
            if (sarray[i].matches("[a-zA-Z]")) {
                intarray[i] = (int) sarray[i].toUpperCase().charAt(0) - 55;
            } else {
                intarray[i] = Integer.parseInt(sarray[i]);
            }
        }
        return intarray;
    }
    return null;
}
share|improve this answer
That isn't going to work - you need to call toUpper() before subtracting I think – Jeff Dec 5 '12 at 7:36
1  
I know exactly where that magic number 55 comes from...but could you elaborate on it a bit? It's not immediately clear to others at first glance. – Makoto Dec 5 '12 at 7:37
@Makoto Thanks for suggestions.I have updated my answer accordingly. – Abhi_Mishra Dec 5 '12 at 7:40
Still needs to be toUppered. Otherwise "a" will result in 42 - which may be the answer to life, the universe and everything, but is probably not the desired answer... – Jeff Dec 5 '12 at 7:43
@Jeff Ofcourse it is desired answer..But now..:)..Thanks – Abhi_Mishra Dec 5 '12 at 7:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.