Results 1 to 9 of 9
Thread: My own string class
- 01-22-2010, 07:52 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 16
- Rep Power
- 0
My own string class
This is my first time working in java, my only other programming experience was a semester long class in C++. Our first project in java is to basically mimic the String class that already exists in java. I have been having an issue with creating one method, to return the length of the user's string. The way I'm thinking of doing it is to convert the string to a character array, then increment a counter as it went through all the characters. Two issues though, one, how would I go about converting the string to a char array, and two, how will I know when to stop the loop since I don't know how long the string is yet?
Thanks for your time!
- 01-22-2010, 08:12 PM #2
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
You could just use array.length
Also, you should check out the API. It has exactly what you are looking for. I'm not going to tell you what to use, because then you won't go look at the API for it.
- 01-22-2010, 08:22 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 16
- Rep Power
- 0
Ha of course. Would I be looking for a method in the array section? I don't think we can use any methods from the String class, like toCharArray();. Unless of course I misunderstood my professor's directions.
- 01-22-2010, 11:18 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
oh, duh, of course you can't use string. Apologies.
The array.length call will still work for what you are using.
So the question is, what do you have in your class that you are calling length on? If your class can't use string, I'm assuming you just have a bunch of chars. If you have a string within your class, that would presumably be cheating?
- 01-23-2010, 12:10 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 16
- Rep Power
- 0
Our prof is giving us a main to put into our class to test all of our methods. For example to test the length method,
Java Code:MyString ms1 = new MyString ("Hello"); System.out.println ("The length of ms1 is: " + ms1.length());
Java Code:for(int i =0; i>userString.length();i++) userArray[i]=userString[i];
- 01-23-2010, 12:12 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
How is your constructor for the String class?
something like ?:
Java Code:public String(Char[] args) { // deal with char array }
Last edited by Newbie666; 01-23-2010 at 12:29 AM.
- 01-23-2010, 12:24 AM #7
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
[QUOTE=viperlasson;101348]Our prof is giving us a main to put into our class to test all of our methods. For example to test the length method,
Java Code:MyString ms1 = new MyString ("Hello"); System.out.println ("The length of ms1 is: " + ms1.length());
Java Code:for(int i =0; i>userString.length();i++) userArray[i]=userString[i];
Java Code:public class MyString { private char[] array = null; public MyString(char[] array){ this.array = array; } public int length(){ return this.array.length; } public MyString subString(int begin){ char[] temp = new char[this.array.length-begin]; for(int i = begin; i < this.array.length; i++){ temp[i-begin] = this.array[begin]; } return new MyString(temp); } }
Last edited by senorbum; 01-23-2010 at 12:25 AM. Reason: Add code tags
- 01-23-2010, 12:33 AM #8
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
that looks good, all I could think to suggest a Class CharArrayWriter which would have been a backwards way of doing things :P
- 01-25-2010, 06:55 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 16
- Rep Power
- 0
Thanks for all the help so far. Yes , the prof told us to use character arrays when implementing all of our methods. Since some of the constructors use a string in their parameters, I'm in need of converting that string to an array of characters in order to manipulate it. The way im looking at how to do this is a method called
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
I really don't know if we are allowed to use this method since it is apart of the String class, but I don't know any other way to do this so far. My code for taking a string from a parameter and making it an array of characters.
Java Code:... private string userString; private char userArray [] = new char; ... MyString(String st1) { int length = st1.length(); st1.getChars(0,length,userArray,0); }
Similar Threads
-
Driver, Class, and Connection String to UltraLite 11
By Harrie_KalaChakra in forum EclipseReplies: 0Last Post: 01-06-2010, 05:33 AM -
Pass String[] into method in different class
By Swankee in forum New To JavaReplies: 19Last Post: 09-28-2009, 06:30 PM -
getting the method and the class names (as argument, String varialbe)
By itaipee in forum New To JavaReplies: 4Last Post: 03-03-2009, 10:39 AM -
How to create object dynamically with class name known in string format
By ranu_gokhe in forum Advanced JavaReplies: 1Last Post: 04-09-2008, 03:15 AM -
EXTENDING the string class
By ferno in forum New To JavaReplies: 3Last Post: 12-11-2007, 10:41 PM
Bookmarks