1. Post #1
    Gold Member
    Nick Lomax's Avatar
    August 2006
    957 Posts
    Hey. Im working on an assignment at my university, and Im kinda stuck at one point. The user is supposed to put in a coordinate like 0,0 or 1,1 etc. Except that the user should put it in like this 1A, 1B, 1C etc. The array is [3][6].

    So the input should be 0 to 2 and A to F. (its supposed to be different apartments in a building.

    Hope you understand my qeustion. I know I have to use split or something like that, but Ive never used split so Im not sure how to.


    EDIT

    Actually, it would be easier to give each of the coordinate a name in the start of the class. Like 0,0 would be 1A, 0,1 would be 1B etc... I guess I know one way of doing it but it would require too much code. I know there is a shorter code to do it, so Im hoping someone here would know.

  2. Post #2
    Darkest_97's Avatar
    February 2011
    186 Posts
    To clear something up, (0, 0) would be 0A would it not? If A is 0, then 0 would be 0, not 1 like you said.

    To split strings up you can use split and give it the character to split on. So like str.split(","), would split the input string of "0,A" into to array, with "0" in index 0, and "A" in index 1. You might be able to split on "" to separate every character like you would want to do with the input.
    You could also just use substring. str.substring(start index, end index) which would be easier.

    So you get each character separately and with the letter just decide which letter it is and choose the number it represents. You could do it with a bunch of ifs or cases .
    Or you could get the ascii value of the character and subtract 65. "A" has an ascii value of 65, which would be index 0. This way would be easier.

    And to convert the first number in the input to an integer from a string just use:
    Integer.parseInt(string)

    Hopefully I understood the question.

  3. Post #3
    Gold Member
    Nick Lomax's Avatar
    August 2006
    957 Posts
    Well yes 0 would be 0, but in this case it hsould be 1 just like the other one is A. It is floor lvls and apartment letter. We start on floor 1 in Norway. 1st floor is the ground floor. So the user puts in floor and apartment letter. So first apt is 1A and last apt is 3F.

    Anyways. Ill look closer at what your wrote and try to fix it. Thanks for the reply. Im still open for more replies if anyone has a different idea. I always like to see how people solve stuff differently.

  4. Post #4
    Zyx
    Guest 3855 is lost and can't find the park exit
    Zyx's Avatar
    February 2005
    2,838 Posts
    int a = theNumberPart;
    char b = theCharacterPart;
    
    if (b = capital)
    {
    array[a][b-65]
    }
    else
    {
    array[a][b-97]
    }
    

    Explanation:
    the char A can be casted to the number 65 and a to 97. The same way B is 66 and b 98 etc.

    Since it's an assignment in school, you can from this work out exactly how much to subtract so it fits with the floor and room values you need.

    So, basically what was said above, but in pseudo code form.