Results 1 to 13 of 13
Thread: Array problems
- 07-19-2012, 12:08 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
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
-
Re: Array problems
The easiest way is to simply have theLetters = tester.toCharArray()
- 07-19-2012, 12:25 AM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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
-
Re: Array problems
- 07-19-2012, 12:35 AM #5
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
-
Re: Array problems
- 07-19-2012, 01:50 AM #7
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
Re: Array problems
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 :(
-
Re: Array problems
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.
- 07-19-2012, 10:15 AM #9
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
Re: Array problems
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.
- 07-19-2012, 10:22 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-19-2012, 11:25 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 07-19-2012, 01:30 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
- 07-19-2012, 02:39 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Problems inserting 1d array to 2d array grid
By jumpgirl in forum New To JavaReplies: 1Last Post: 03-26-2012, 03:04 AM -
Problems with array
By Desmond in forum New To JavaReplies: 1Last Post: 11-04-2010, 08:14 AM -
Array problems
By braddy in forum New To JavaReplies: 4Last Post: 10-09-2010, 01:18 PM -
Array problems..
By smokeviolent in forum New To JavaReplies: 1Last Post: 04-17-2009, 06:45 AM -
Array problems
By Hosticus in forum New To JavaReplies: 2Last Post: 01-18-2009, 02:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks