Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-01-2009, 09:10 PM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
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:

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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-01-2009, 10:08 PM
angryboy's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Javaland
Posts: 743
Rep Power: 2
angryboy is on a distinguished road
Default
a little pseudo code for you.
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)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-01-2009, 10:30 PM
Senior Member
 
Join Date: Mar 2009
Posts: 376
Rep Power: 1
Singing Boyo is on a distinguished road
Default
some more pseudocode...
Code:
class Foo{

     Char[] test
     Constructor(String arg){
          test equals arg converted to char array...//there's a method for this!!!
     }
}
__________________
So you came along and found Java? Randomly?
Well then, you're just like me!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-01-2009, 10:32 PM
angryboy's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Javaland
Posts: 743
Rep Power: 2
angryboy is on a distinguished road
Default
yes, but his new. better to learn the algorithm for this as well...
__________________
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-01-2009, 11:49 PM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
Default
Hey Guys,

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

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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-02-2009, 12:00 AM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
Default
******* posted twice ********

Last edited by fullmetaljacket; 07-02-2009 at 12:47 AM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-02-2009, 12:45 AM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-02-2009, 12:53 AM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-02-2009, 01:04 AM
angryboy's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Javaland
Posts: 743
Rep Power: 2
angryboy is on a distinguished road
Default
It be helpful to post your actual codes so far and not c/p our pseudo codes.
__________________
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)

Last edited by angryboy; 07-02-2009 at 01:10 AM. Reason: spell err :*
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-02-2009, 01:09 AM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
Default
angryboy - I did post my code, at the start of my thread.

Here it is again:

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 01:26 AM.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-02-2009, 01:49 AM
angryboy's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Javaland
Posts: 743
Rep Power: 2
angryboy is on a distinguished road
Default
Originally Posted by fullmetaljacket View Post
angryboy - I did post my code, at the start of my thread.

Here it is again:

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)
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-02-2009, 01:52 AM
Member
 
Join Date: Feb 2009
Posts: 45
Rep Power: 0
JohnnyR is on a distinguished road
Default
Take a look here for more info on toCharArray()
String (Java Platform SE 6)

Your code is going to basically be the same.
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-02-2009, 02:09 AM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
Default
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.
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 07-02-2009, 02:15 AM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 07-02-2009, 02:27 AM
Member
 
Join Date: Feb 2009
Posts: 45
Rep Power: 0
JohnnyR is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 07-02-2009, 02:49 AM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 07-02-2009, 06:47 PM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
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):

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.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 07-02-2009, 09:36 PM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
Default
OK - so far I am starting to think the code should look like this:

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.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 07-02-2009, 09:55 PM
Member
 
Join Date: Mar 2009
Posts: 61
Rep Power: 0
fullmetaljacket is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 07-03-2009, 12:03 AM
Member
 
Join Date: Feb 2009
Posts: 45
Rep Power: 0
JohnnyR is on a distinguished road
Default
You are very close in fact

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 :
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Creating an Array of Arrays? xcallmejudasx Advanced Java 5 11-04-2008 07:01 PM
Calling constructor of parent class from a constructor Java Tip Java Tips 0 12-19-2007 10:10 AM
Calling constructor of same class from a constructor Java Tip Java Tips 0 12-19-2007 10:01 AM
repetition of 'arguments'(?) Igor New To Java 3 12-13-2007 11:08 AM
Variable No. of Arguments Gajesh Tripathi New To Java 2 10-31-2007 03:50 PM


All times are GMT +2. The time now is 04:35 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org