Results 1 to 13 of 13

Thread: Array problems

  1. #1
    walsby14 is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Array problems

    ok so heres my code so far:

    public class simpleString
    {
    private char[] theLetters;
    public simpleString (String tester) {
    theLetters = new char[tester.length()];
    }
    }

    I'm trying to create a constructor which takes an argument of type String. It should create an array of char which is the same size as the argument in the constructor. I think i've done this correctly. What i need to do now is copy the letters of the argument and put them in the array one at a time, in order.

    Completely stuck here so any help and explanation is much appreciated :D

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

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Array problems

    Scour the API for String
    String (Java Platform SE 6)
    There are several methods you can use the get the char array of a String (search for the methods that return or accept a character array)
    Edit: too late again

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

    Default Re: Array problems

    Quote Originally Posted by doWhile View Post
    Scour the API for String
    String (Java Platform SE 6)
    There are several methods you can use the get the char array of a String (search for the methods that return or accept a character array)
    Edit: too late again
    Sorry, and your way (referring him to the API) was better than mine.

    But then again, I wonder if as part of this assignment he is supposed to write his own code to do this. If so, the original poster should investigate using a for loop.

  5. #5
    walsby14 is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Array problems

    Quote Originally Posted by Fubarable View Post
    Sorry, and your way (referring him to the API) was better than mine.

    But then again, I wonder if as part of this assignment he is supposed to write his own code to do this. If so, the original poster should investigate using a for loop.

    Yes i think i have to use a for loop to get each character and put it in the array.

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

    Default Re: Array problems

    Quote Originally Posted by walsby14 View Post
    Yes i think i have to use a for loop to get each character and put it in the array.
    Then by all means give it a go. If you get stuck with the attempt, show us what you've tried and any error messages that you might get. Have a look at example for loops to see how to set this up correctly.

  7. #7
    walsby14 is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Array problems

    Quote Originally Posted by Fubarable View Post
    Then by all means give it a go. If you get stuck with the attempt, show us what you've tried and any error messages that you might get. Have a look at example for loops to see how to set this up correctly.
    Right I've had a look and this has really got me stumped, i checked the link you guys sent me and i think i need to use the charAt or something but i really can't see how i set it up in a for loop, i now the structure of the for loop but i just can't think how to do it :(

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

    Default Re: Array problems

    Quote Originally Posted by walsby14 View Post
    Right I've had a look and this has really got me stumped, i checked the link you guys sent me and i think i need to use the charAt or something but i really can't see how i set it up in a for loop, i now the structure of the for loop but i just can't think how to do it :(
    We could whip up a solution in 3 seconds, but then you wouldn't learn anything, and half of learning to program is getting through this initial struggle, learning how to think for your self. If you know the structure of a for, then please at least give it a try. You're not going to break your computer by experimenting.

  9. #9
    walsby14 is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Array problems

    Quote Originally Posted by Fubarable View Post
    We could whip up a solution in 3 seconds, but then you wouldn't learn anything, and half of learning to program is getting through this initial struggle, learning how to think for your self. If you know the structure of a for, then please at least give it a try. You're not going to break your computer by experimenting.
    ok so this is what I've gone for:

    private char[] theLetters;
    public simpleString (String tester) {
    theLetters = new char[tester.length()];
    for (int i = 0; i < tester.length(); i++) {
    char[]charAt;

    when i run it however it comes up with an incredibly long error message.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default Re: Array problems

    Throw away that method (it doesn't even compile) and read the API documentatio for the String class; it has a method that can produce an array of chars that contain the chars from the String (and it works ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Array problems

    But as said earlier, it appears they are supposed to use a loop (ie using the inbuilt method is a 'cheat').
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default Re: Array problems

    Quote Originally Posted by Tolls View Post
    But as said earlier, it appears they are supposed to use a loop (ie using the inbuilt method is a 'cheat').
    But, but, but ... that method also uses a loop so the OP's program also uses a loop, just not its own ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Array problems

    You know that and I know that.
    Got to learn this stuff somehow I suppose...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Problems inserting 1d array to 2d array grid
    By jumpgirl in forum New To Java
    Replies: 1
    Last Post: 03-26-2012, 04:04 AM
  2. Problems with array
    By Desmond in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 09:14 AM
  3. Array problems
    By braddy in forum New To Java
    Replies: 4
    Last Post: 10-09-2010, 02:18 PM
  4. Array problems..
    By smokeviolent in forum New To Java
    Replies: 1
    Last Post: 04-17-2009, 07:45 AM
  5. Array problems
    By Hosticus in forum New To Java
    Replies: 2
    Last Post: 01-18-2009, 03:48 AM

Posting Permissions

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