Results 1 to 20 of 22
Thread: stumped......
- 07-12-2009, 08:51 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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; } } }
-
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.
- 07-12-2009, 10:24 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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.
-
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
- 07-13-2009, 01:16 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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) { ???; } }
-
Let's break some of this down.
This is good as you're declaring your array as a field in the class, and it is visible throughout the class.Java Code:private char[] theLetters;
Your constructor's "signature" here is fine.Java Code:public SimpleString(String aString) {
The super() call is unnecessary for this simple class that extends Object.Java Code:super();
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.Java Code:char[] theLetters = new char[aString.length()];
Last edited by Fubarable; 07-13-2009 at 02:44 AM.
- 07-13-2009, 11:33 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
- 07-13-2009, 12:46 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 07-13-2009, 01:00 PM #9
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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); } }
- 07-13-2009, 01:15 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
So, how are you checking that theLetters is being built correctly?Java Code:for (int i = 0; i < theLetters.length; i++) { System.out.print(theLetters[i]); }
- 07-13-2009, 01:41 PM #11
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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; }
- 07-13-2009, 01:56 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 07-13-2009, 02:13 PM #13
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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]; } }
- 07-13-2009, 02:21 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 07-13-2009, 02:29 PM #15
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
- 07-13-2009, 02:51 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 07-13-2009, 03:04 PM #17
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
- 07-13-2009, 03:13 PM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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?
- 07-13-2009, 03:34 PM #19
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
- 07-13-2009, 03:49 PM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks