Page 1 of 2 12 LastLast
Results 1 to 20 of 39
  1. #1
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default Creating a constructor with arrays and arguments

    Dear All,

    I'm having some problems again! I am trying to create a constructor with the following array requirements.

    Firstly - I have created a private instance variable called test that is suitable for holding an array of characters.

    private char[] test;

    I am then asked to create a constructor that takes a single argument of type String. The constructor should create an array of char which is the same size as the constructors argument and assign it to test. Then the code should copy all the letters from the argument into the array referenced by test in the same order.

    I have:

    Java Code:
    public myString(String[] args)
    {
       this.test = args;
    }
    I get the error wrong type. This I understand. I need to cast the String argument to type char. But I only have examples of using CharAt(index), which will return only one character not all as Char.

    Can anyone let me know where and what I have done wrong.

    All help gratefully accepted.

    All the best - FMJ.

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    a little pseudo code for you.
    Java Code:
    class Foo{
      Field Char[] test;
    
      constructor Foo(String arg){
        for each letter in arg,
          assign it to test[index]
      }
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    some more pseudocode...
    Java Code:
    class Foo{
    
         Char[] test
         Constructor(String arg){
              test equals arg converted to char array...//there's a method for this!!!
         }
    }
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    yes, but his new. better to learn the algorithm for this as well...
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hey Guys,

    Yes, I understand that there is a method called toCharArray(), but how can I use it in this constructor?

    Java Code:
    class Foo{
      Field Char[] test;
    
      constructor Foo(String arg)
     {
        this.test = toCharArray(arg);
     }
    I'm unsure on the correct way to write this? My blooming course books have no reference to this what so ever :-)

    Cheers - FMJ.

  6. #6
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    ******* posted twice ********
    Last edited by fullmetaljacket; 07-01-2009 at 11:47 PM.

  7. #7
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Sorry - my internet connection is up and down today. It seems to have posted my comment twice.

    I understand that there is a method to convert strings to char, but I'm unsure on how to use this within my code example as my course books have no examples of this.

    Please help :-)

    Cheers - FMJ.

  8. #8
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Also - for your information. I am being asked to pretend the Java String class does not exist and thus writing my own. I am only allowed to use charAt() and length(), I cannot make use of methods from the Arrays and Array utility classes.

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    It be helpful to post your actual codes so far and not c/p our pseudo codes.
    Last edited by angryboy; 07-02-2009 at 12:10 AM. Reason: spell err :*
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    angryboy - I did post my code, at the start of my thread.

    Here it is again:

    Java Code:
    public myString(String[] args)
    {
       this.test = args;
    }
    I know this is incorrect. I am also aware of a toCharArray() method, but unsure on how to use it. I need to create a constructor that takes a single argument of type String. The constructor should create an array of char which is the same size as the constructors argument and assign it to test. Then your code should copy all the letters from the argument into the array referenced by test in the same order.

    I have already created a private instance variable called test that is suitable for holding an array of characters. See first post.

    Any help gratefully accepted.

    Cheers - FMJ
    Last edited by fullmetaljacket; 07-02-2009 at 12:26 AM.

  11. #11
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Quote Originally Posted by fullmetaljacket View Post
    angryboy - I did post my code, at the start of my thread.

    Here it is again:

    Java Code:
    public myString(String[] args)
    {
       this.test = args;
    }
    I know this is incorrect. ...
    Yes but what have you done after I gave you the pseudo code? what changes have you made?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  12. #12
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    Take a look here for more info on toCharArray()
    String (Java Platform SE 6)

    Your code is going to basically be the same.
    Java Code:
    public myString(String args)
    {
       this.test = args.toCharArray();
    }
    Also your input statement was a String array, from your last post it should just be String.

  13. #13
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by JohnnyR View Post
    Take a look here for more info on toCharArray()
    String (Java Platform SE 6)

    Your code is going to basically be the same.
    Java Code:
    public myString(String args)
    {
       this.test = args.toCharArray();
    }
    Also your input statement was a String array, from your last post it should just be String.
    JonnyR! Thanks for the info mate. It seems to be able to compile now.

    I know know how to use the toCharArray() method!

    All the best,

    FMJ.

  14. #14
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    The only problem is, that in the question it states that the only String messages you are allowed to use is charAt() and length().

    Is there anyway that chartAt() can be made to do the same thing?

    Regards - FMJ.

  15. #15
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    Yes there is, but as I assume this is an assignment of some kind?

    Basically toCharArray puts the entire string as an array of chars, what charAt does it gets a character at a certain indexing point (starting at 0)
    e.g. the word : "some" would be indexed as 0 1 2 3 (s = 0) (e=3) etc.

    so the statement args.charAt(0) would get you the first character in the string.

    You can use a for loop and use the int value of the loop as your indexing eg start at 0 and increment by 1 each loop. Make sure you get the clause correct or you may get a null pointer.

    tiny pseudo - get char at position, set char into char[] at same position.

    Sorry I may of just confused you.. I find it hard with out giving you the exact code to get it to work...

  16. #16
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hi JohnnyR :-)

    Thanks for the info mate. I will check this out in the morning with a fresh head :-)

    Thank you all for your help and comments it really is greatly appreciated!

    All the best - FMJ.

  17. #17
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hey JohnnyR :-)

    I have been working with this all day, but I'm still very confused. I understand your earlier suggestions in concept, but my coursebooks do not show me an example of using for with two variables. Let me explain in greater detail:

    I need to create a constructor that takes a single argument of type String.

    public myString(String args)

    The constructor should create an array of char which is the same size as the constructors argument and assign it to test array (which is of type Char). Then your code should copy all the letters from the argument into the array referenced by test in the same order.

    So I have think I have two variables, the String length of the argument (args) which could vary and the need to cast each String character to Char?

    My example codes do not seem to touch on this for example (a very different example):

    Java Code:
    double[] hoursWorked = new double[52];
    for (int i = 0; i < hoursWorked.length; i++)
    {
        hoursWorked[i] = 40.0;
    }
    So I can see that test.length would return the size of the array so this might be appropriate in the for statement? But this (above) only deals with an array that has already been created and makes changes. I need to take a String agrument, convert each character to Char and then insert into the test array (suitable for Char).

    Please if you can help, I would be so grateful. I'm confident that once I have cracked this, I can rocket on with my questions. But at the moment - I am totally lost! Totally.

    All the best (and I have tried to PM as well)

    FMJ.

  18. #18
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    OK - so far I am starting to think the code should look like this:

    Java Code:
    public myString(String args)
    {
       for(int i = 0; < args.length; i++)
    After this I am lost, maybe this is incorrect as well? Ho Hum....!

    Any help is really needed :-)

    Cheers - FMJ.

  19. #19
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    also if I create the int variable i and set this to the value of 0, I can use charAt(i) to return a char value of i.

    But I'm still very lost.

    Cheers - FMJ.

  20. #20
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    You are very close in fact :)

    Java Code:
    public myString(String args) {
       for (int i = 0; < args.length; i++) {
                char temp = args.atChar(i); //get the character you want store temp
                test[i] = temp; //sets your char[] test at position i to the char in temp.
                 // alternative way to do it in 1 call is test[i] = args.atChar(i);
       }
    }
    hope this helps :)

    btw sorry im in Aussie so my reponses re delayed :

Page 1 of 2 12 LastLast

Similar Threads

  1. [SOLVED] Creating an Array of Arrays?
    By xcallmejudasx in forum Advanced Java
    Replies: 5
    Last Post: 11-04-2008, 06:01 PM
  2. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  4. repetition of 'arguments'(?)
    By Igor in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 10:08 AM
  5. Variable No. of Arguments
    By Gajesh Tripathi in forum New To Java
    Replies: 2
    Last Post: 10-31-2007, 02:50 PM

Posting Permissions

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