Results 1 to 9 of 9
  1. #1
    viperlasson is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default 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!

  2. #2
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    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.

  3. #3
    viperlasson is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default

    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.

  4. #4
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    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?

  5. #5
    viperlasson is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default

    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());
    would be used. I made one of the constructors store a String value in the parameters so Hello will be in the private string variable userString. Now about changing userString into a character array userArray...He mentioned something about going through the string, pulling out each char and sticking it into the array until you get to the null character. But how do you loop through a string?

    Java Code:
    for(int i =0; i>userString.length();i++)
        userArray[i]=userString[i];
    Would be my guess but I don't think you can do that with strings.

  6. #6
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    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.

  7. #7
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    [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());
    would be used. I made one of the constructors store a String value in the parameters so Hello will be in the private string variable userString. Now about changing userString into a character array userArray...He mentioned something about going through the string, pulling out each char and sticking it into the array until you get to the null character. But how do you loop through a string?

    Java Code:
    for(int i =0; i>userString.length();i++)
        userArray[i]=userString[i];
    So this assignment I assume is meant to implement the various methods of string using char arrays? If so, the following is how I would do things. I don't normally provide code, but you clearly are trying to do things, and its likely not a complete solution. Also, if you are doing things differently then using char arrays, this will provide a simple guideline on how to do it.
    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

  8. #8
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    that looks good, all I could think to suggest a Class CharArrayWriter which would have been a backwards way of doing things :P

  9. #9
    viperlasson is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default

    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);
    }
    So assuming my code is right, this will pull all the chars out of st1, and store it into my private variable userArray which is in the class.

Similar Threads

  1. Driver, Class, and Connection String to UltraLite 11
    By Harrie_KalaChakra in forum Eclipse
    Replies: 0
    Last Post: 01-06-2010, 05:33 AM
  2. Pass String[] into method in different class
    By Swankee in forum New To Java
    Replies: 19
    Last Post: 09-28-2009, 06:30 PM
  3. Replies: 4
    Last Post: 03-03-2009, 10:39 AM
  4. Replies: 1
    Last Post: 04-09-2008, 03:15 AM
  5. EXTENDING the string class
    By ferno in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 10:41 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
  •