2

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

2
  • so what is the question? you want to optimize the code or you are getting some problems? Commented Dec 5, 2012 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. Commented Dec 5, 2012 at 8:05

3 Answers 3

5

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
3
  • Probably the easiest and most concise answer here. Commented Dec 5, 2012 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. Commented Dec 5, 2012 at 8:10
  • @madhatter, you would need to pass each letter through Integer.parseInt Commented Dec 5, 2012 at 9:38
2

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.
7
  • @Jeff Ascii for "Z" is 122 so we should subtract 87 to get 35, op specifies that. Commented Dec 5, 2012 at 7:47
  • Yeah, I just realised that and was changin my comment when you posted yours. My bad... Commented Dec 5, 2012 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? Commented Dec 5, 2012 at 8:01
  • @madhatter I said that "assuming user doesn't enter empty string" in my post above and it seems that your string is empty, i.e. temp[i] = ""; user should just enter digits(0-9) or characters(a-z and A-Z), in that case my code works perfect. Commented Dec 5, 2012 at 8:07
  • When I ran the code I entered the string asdf and that was what was returned could it be how the string array is saved and split? Commented Dec 5, 2012 at 8:11
1

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;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.