can't explain output from my String.charAt(int j) method

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

can't explain output from my String.charAt(int j) method

Rather Iffy
This post was updated on .
When i run  my String class together with the provided Main class and OS vm files
my charAt method show strange behavior.
 
   /** Returns the character at location j. */                                    
      method char charAt(int j) {                                                      
         return txt[j];                                                            
    }  
 
I get this VMEmulator output :



My problem is with the charAt[2] value 599.
The prefix 5 is related with the object field length and/or the length method.
I tried  a string with 4 characters and the prefix also changed to 4.

My length method is :
  /** Returns the current length of this String. */                    
     method int length() {                                                            
      return length;                                                                    
    }

One of the suspects is  my constructor . I found a solution to the zero capacity string: only declare the array, in the charAt method referred to as txt, and let it alone.
Maybe a little bit tricky. But it got through the test. And the string in the case of this charAt test has a size of 6 anyway.

By the way there is another problem with the test of the negative integer as you can see.
I can solve that very easily with an additional dispose and new call, but i don't know
if that counts as a 'proper' solution.

I would appreciate help on these problems.
--Chrit

Reply | Threaded
Open this post in threaded view
|

Re: can't explain output from my String.charAt(int j) method

cadet1620
Administrator
Rather Iffy wrote
I get this VMEmulator output :

Your charAt() looks like mine, except that you should check that 'j' is out of range and do Sys.err(15).

I'd check setInt().

The second 'setInt:' should print -32767.  I looks like you didn't clear the existing value before setting the new value, and also that you negative conversion didn't work correctly and was appending not-printable characters.  (And did not sys error when it ran off the end of the text buffer.)

The strange value for 'charAt:' also implicates setInt() because printInt() uses setInt(), and the previous printInt() printed '5'.

-Mark


Reply | Threaded
Open this post in threaded view
|

Re: can't explain output from my String.charAt(int j) method

Rather Iffy
This post was updated on .
Thanks I will check it in the morning.
--Chrit

Initializing length to 0 in setInt solved the  first problem.
After declaration of string with zero capacity i initialized it txt field to null just to be sure.

The problem with the negative integers still stands.
 
--Chrit