Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: stumped......

  1. #1
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default stumped......

    Hello All,

    I need to write the constructor for a class called SimpleString which acts in a similar way to the String class. The plan is for the constructor to create an array of char which is the same size as the constructor's argument and assign it to the instance variable theLetters. Then the code should copy all the letters from the String argument into the array referenced by theLetters in the same order. This I am attempting to do with a for loop, and below is the code I have so far, but the compiler is telling me "incompatible types - found char but expected java.lang.String". I can sort of see why this error is being reported but I cannot see how else to achieve the aim......
    Can anyone help please? Many thanks.

    Java Code:
    public class SimpleString
    {
       
       private String[] theLetters;
    
    
       public SimpleString(String aString)
       {
          super();
          for (int count = 0;count < aString.length();count++)
          {
             theLetters[count] = aString.charAt(count);
             count = count + 1;
          }
       }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your instructions tell you to create an array of char, and yet you create an array of String. There's a big difference between the two.

    Also, you need to initialize your class's array using the String's length() method.

  3. #3
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your instructions tell you to create an array of char, and yet you create an array of String. There's a big difference between the two.

    Also, you need to initialize your class's array using the String's length() method.
    Aha yes! I have now declared the theLetters instance variable as char rather than String. What would the condtructor statement now look like? I've tried a few variations but have only received NullPointerException warnings.
    Also how would you incorporate the length() method in the class header?
    Thanks for your support.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to initialize your class's char[] array in the constructor. Have you done this before, initializing arrays? i.e.,

    Java Code:
    int[] myArray;   // declares an array
    myArray = new int[10];  // initializes the array

  5. #5
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You need to initialize your class's char[] array in the constructor. Have you done this before, initializing arrays? i.e.,

    Java Code:
    int[] myArray;   // declares an array
    myArray = new int[10];  // initializes the array
    I've dabbled with arrays in Javascript but I'm new to arrays in Java.
    This is the code I have now with which I can generate new instances of the class with a line like - SimpleString testString = new SimpleString("stringvest");
    However when I try to inspect the theLetters array (which should have been created by the constructor statement?) it throws a semantic error Not An Array.
    I have also added the shell for a second constructor which uses a char[] array as an argument but I cannot simply assign the argument to theLetters. Instead the code needs to take a similar form to that of the class header?

    Java Code:
    public class SimpleString
    {
       private char[] theLetters;
    
    
       /**
        * Default constructor for objects of class SimpleString
        */
       public SimpleString(String aString)
       {
          super();
          char[] theLetters = new char[aString.length()];
          for (int count = 0;count < aString.length();count++)
          {
             theLetters[count] = aString.charAt(count);
             count = count + 1;
          }
       }
       
       /**
        * Second constructor for objects of class SimpleString
        */
    
       public SimpleString(char[] aChar)
       {
          ???;
       }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Let's break some of this down.
    Java Code:
       private char[] theLetters;
    This is good as you're declaring your array as a field in the class, and it is visible throughout the class.

    Java Code:
       public SimpleString(String aString)
       {
    Your constructor's "signature" here is fine.

    Java Code:
          super();
    The super() call is unnecessary for this simple class that extends Object.

    Java Code:
          char[] theLetters = new char[aString.length()];
    Here's a problem. You initialize the array fine with call to new and you've given it a proper length, but you are redeclaring the array with "char[]". This creates a new array that is completely different from your class field and that in fact "shadows" or obscures the class field. Anything you do with this array will not be reflected in the class field (the theLetters array declared at the beginning of your class) and in fact it will remain null. The key here is to assign the array without declaring it again. Please see my example above that shows how to assign and how to declare.
    Last edited by Fubarable; 07-13-2009 at 03:44 AM.

  7. #7
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Let's break some of this down.
    Java Code:
       private char[] theLetters;
    This is good as you're declaring your array as a field in the class, and it is visible throughout the class.

    Java Code:
       public SimpleString(String aString)
       {
    Your constructor's "signature" here is fine.

    Java Code:
          super();
    The super() call is unnecessary for this simple class that extends Object.

    Java Code:
          char[] theLetters = new char[aString.length()];
    Here's a problem. You initialize the array fine with call to new and you've given it a proper length, but you are redeclaring the array with "char[]". This creates a new array that is completely different from your class field and that in fact "shadows" or obscures the class field. Anything you do with this array will not be reflected in the class field (the theLetters array declared at the beginning of your class) and in fact it will remain null. The key here is to assign the array without declaring it again. Please see my example above that shows how to assign and how to declare.
    just removing char[] gives an error so clearly it is not as simple as that?

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default

    Quote Originally Posted by trueblue View Post
    just removing char[] gives an error so clearly it is not as simple as that?
    What error?
    It compiles.

    However, in your loop, you are incrementing count twice...once in the for declaration with count++, and again at the end of the loop with count = count + 1.

  9. #9
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    What error?
    It compiles.

    However, in your loop, you are incrementing count twice...once in the for declaration with count++, and again at the end of the loop with count = count + 1.
    Yes I realised that I was incrementing count incorrectly so removed the line.
    Now I have the following code which will allow me to create a new instance of SimpleString, but it does not initialise the theLetters array correctly as the array only seems to exist within the for loop?

    Java Code:
    public class SimpleString
    {
       
       private char[] theLetters;
    
    
       public SimpleString(String aString)
       {
          super();
          theLetters = new char[aString.length()];
          for (int count = 0;count < aString.length();count++)
          {
             theLetters[count] = aString.charAt(count);
          }
       }

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default

    Quote Originally Posted by trueblue View Post
    Yes I realised that I was incrementing count incorrectly so removed the line.
    Now I have the following code which will allow me to create a new instance of SimpleString, but it does not initialise the theLetters array correctly as the array only seems to exist within the for loop?

    Java Code:
    public class SimpleString
    {
       
       private char[] theLetters;
    
    
       public SimpleString(String aString)
       {
          super();
          theLetters = new char[aString.length()];
          for (int count = 0;count < aString.length();count++)
          {
             theLetters[count] = aString.charAt(count);
          }
       }
    Copying that, and sticking the following right after your for loop, results in the correct characters being printed out. So that shows that theLetters is not just in the for loop.

    Java Code:
    	      for (int i = 0; i < theLetters.length; i++)
    	      {
    		      System.out.print(theLetters[i]);
    	      }
    So, how are you checking that theLetters is being built correctly?

  11. #11
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Copying that, and sticking the following right after your for loop, results in the correct characters being printed out. So that shows that theLetters is not just in the for loop.

    Java Code:
    	      for (int i = 0; i < theLetters.length; i++)
    	      {
    		      System.out.print(theLetters[i]);
    	      }
    So, how are you checking that theLetters is being built correctly?
    i was trying lines like this - System.out.println(theLetters[0]);
    but it was returning a semantic error - not an array.
    your test method clearly works thanks, why won't mine?

    i now have to create a second constructor which uses a char array argument, but cannot just assign the argument to theLetters as below.
    The only String messages I can use are charAt() and length() and I cannot use methods from the Arrays and Array utility classes. is this possible?

    Java Code:
    public SimpleString(char[] aChar)
       {
          theLetters = aChar;
       }

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default

    Quote Originally Posted by trueblue View Post
    i was trying lines like this - System.out.println(theLetters[0]);
    but it was returning a semantic error - not an array.
    your test method clearly works thanks, why won't mine?
    That's a very good question, but without seeing your code for this I can't really answer it. theLetters[0] is perfectly valid.

    Quote Originally Posted by trueblue View Post
    i now have to create a second constructor which uses a char array argument, but cannot just assign the argument to theLetters as below.
    The only String messages I can use are charAt() and length() and I cannot use methods from the Arrays and Array utility classes. is this possible?

    Java Code:
    public SimpleString(char[] aChar)
       {
          theLetters = aChar;
       }
    This should be almost identical to what you've just done with the String parameter.

  13. #13
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    That's a very good question, but without seeing your code for this I can't really answer it. theLetters[0] is perfectly valid.



    This should be almost identical to what you've just done with the String parameter.
    this is the code i have which compiles ok but i can't seem to write a constructor statement that will create an instance of simpleString?

    Java Code:
    public SimpleString(char[] aChar)
       {
          super();
          theLetters = new char[aChar.length];
          for (int count = 0;count < aChar.length;count++)
          {
             theLetters[count] = aChar[count];
          }
       }

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default

    Quote Originally Posted by trueblue View Post
    this is the code i have which compiles ok but i can't seem to write a constructor statement that will create an instance of simpleString?
    How are you trying to construct your SimpleString object then?

  15. #15
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    How are you trying to construct your SimpleString object then?
    SimpleString test = new SimpleString(???);

  16. #16
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default

    Quote Originally Posted by trueblue View Post
    SimpleString test = new SimpleString(???);
    So you need to replace the ??? with either a char[] object, to use that constructor, or a String to use the other constructor. The String could simply be "This is a test string" in place of the question marks.

  17. #17
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    So you need to replace the ??? with either a char[] object, to use that constructor, or a String to use the other constructor. The String could simply be "This is a test string" in place of the question marks.
    this kind of thing just reports errors?

    Java Code:
    SimpleString test = new SimpleString(char[] aChar);
    Java Code:
    SimpleString test = new SimpleString(aChar);

  18. #18
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default

    Quote Originally Posted by trueblue View Post
    this kind of thing just reports errors?

    Java Code:
    SimpleString test = new SimpleString(char[] aChar);
    Java Code:
    SimpleString test = new SimpleString(aChar);
    Well, the first one isn't correct Java. The second one would be correct if you had declared aChar somewhere as a char[]...and initialised it.

    Also, don't flail...think about what you're doing. You're trying to construct an object of char[] to pass into the constructor of SimpleString.

    Do you know how to declare and initialise an array?

  19. #19
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Well, the first one isn't correct Java. The second one would be correct if you had declared aChar somewhere as a char[]...and initialised it.

    Also, don't flail...think about what you're doing. You're trying to construct an object of char[] to pass into the constructor of SimpleString.

    Do you know how to declare and initialise an array?
    OK so say for arguments sake i declared and initialised an array like this.
    how would this help? i realise that it needs to be initialised with a length somehow.

    Java Code:
    char[] aChar = new char[]

  20. #20
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default

    Quote Originally Posted by trueblue View Post
    OK so say for arguments sake i declared and initialised an array like this.
    how would this help? i realise that it needs to be initialised with a length somehow.

    Java Code:
    char[] aChar = new char[]
    As with other arrays, you can simply declare the values in it, and avoid explicitly declaring a size:

    Java Code:
    char[] aChar = new char[] {/*Put your individual characters in here in single quotes separated by commas*/};

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •