Link to home
Start Free TrialLog in
Avatar of ashleycoker
ashleycoker

asked on

Strings and Chars

I have a function I am trying to write:

 public String ConvertTo83(String name) {
     //Returns the Eight.Three filename version of name
        String temp;
        char result[] = new char[11];
        temp = name.trim();
        if (temp.length() > 0)
        {
            temp = temp.toUpperCase();
            if(temp.indexOf(".") == -1){
                for( int x = 0; x < temp.length(); x ++){
                     result[x] = temp.charAt(x);
                }
                for(int x = temp.length(); x < 11; x++)
                    result[x] = 0x20;
            }
            else
            {
                int y = temp.indexOf(".");
                for( int z = 0; z < y; z++)
                {
                    result[z] = temp.charAt(z);
                }
                //result[y] = '.';
                for( int z = y; z < 11; z++)
                {
                    result[z] = temp.charAt(z);
                }
            }
        }
        return result.toString();
    }

However, the line :

     result[x] = temp.charAt(x);

doesn't appear to be working.  I am using Netbeans IDE to write this in, and trhe debugger shows the value of each of the chars of result to have the value '???'.  This is obviously wrong.  As I stp through the code, the line is executed without any errors, but the charAt(x) value is not placed in the result[z] position.  I don't understand why thid would be happening??

please help
Thanks
Ashley
Avatar of twobitadder
twobitadder

for( int x = 0; x < temp.length(); x ++){

should be  for( int x = 0; x < (temp.length() -1); x ++){

remember that arrays start at 0 but you return the number of characters in the string using temp.length.

so if you return 5 for length of "hello"
you want to cycle through elements 0-4

You just overshoot by 1.
actually that's wrong sorry, ignore that.
Avatar of ashleycoker

ASKER

it doesn;t even work on the first pass through.  I just dont understand.  It seems a simple operation.
Hi,
the problem is the line:

 char result[] = new char[11];

you create a bounded array so your method will try and access as many characters as there are in the original string and assign those to the corresponding x position in your array.

However your array can only hold 11 chars so if the string is over 11 chars after trimming leading and trailing whitespace then you will try to write off the end of the array and throw a  java.lang.ArrayIndexOutOfBoundsException.

Change result so it is initialised like:

char result[];

result = new char[temp.length()]
ASKER CERTIFIED SOLUTION
Avatar of twobitadder
twobitadder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
8-)