-1

main class

public CustomString[] split(char delimiter) {
    // char n;
    char[] split = null;
    int numOfSplit = 1;
    int objectLocation=0;
    for (int h = 0; h < this.data.length; h++) {
        if (delimiter == this.data[h]) {
            numOfSplit++;
        }
    }

    CustomString[] splitter = new CustomString[numOfSplit];

    for (int i = 0; i < this.data.length; i++) {
        if (delimiter == this.data[i]) {
            // n = 32;//unicode for space}

            split = new char[i];
            for (int j = 0; j < split.length; j++) {
                char z = this.data[j];
                split[j] = z;
                objectLocation++;

            }
        }

        splitter[objectLocation] = new CustomString(split);
    }
    return splitter;
}

tester class:

    System.out.print("Delimiter: ");
    String delimiter = input.next();
    char[] deli = delimiter.toCharArray();
    char delim = deli[0];
    System.out.println("Splitted String: " + blah.split(delim));

result: [LCustomString;@55f96302

main class completed

import java.util.Scanner;
import java.util.Arrays;

public class CustomString {
    private char[] data;

    public CustomString() {

    } // no arg constructor

    public CustomString(char[] data) {
        this.data = data;

    } // constructor

    public CustomString changeCase() { // type: CustomString
        char n;
        char[] newArray = new char[this.data.length];// this.data;
        // char charNewA
        for (int i = 0; i < newArray.length; i++) {
            n = this.data[i];
            // char[] number = new char[1];
            if (n >= 65 && n <= 90) {
                n += 32;
            }// if capital char change to lower char
            else if (n >= 97 && n <= 122) {
                n -= 32;
            }// if lower char change to capital char

            // create a new array with n properties
            newArray[i] = n;
        }

        return new CustomString(newArray);

    } // returns a new CustomString where the case of each letter is changed to
        // the opposite

    public char charAt(int index) {
        char charIndex = this.data[index - 1];

        return charIndex;
    } // returns character as a given index

    public int compareTo(CustomString rhs) { // take off comments
        int value = 0;
        int loopSize;
        if (this.data.length < rhs.data.length) {
            loopSize = this.data.length;
        } else {
            loopSize = rhs.data.length;
        }
        for (int i = 0; i < loopSize; i++) {
            char char1 = this.data[i];
            char char2 = rhs.data[i]; // should be equal to 2nd customString
            if (char1 != char2) {
                value = char1 - char2;
                // if (char1 > char2) {
                // value = char1-char2; //pos??
                // } else if (char1 < char2) {
                // value =
                // }

            }
        }
        return value;
    } // compares 2 strings lexographically. return 0 if two same. return + is

    public int compareToIgnoreCase(CustomString rhs) {
        char[] newArrayCompareToIgnoreCase = new char[this.data.length];
        // newArrayCompareToIgnoreCase = this.data;
        char[] newArrayCompareToIgnoreCase2 = new char[rhs.data.length];
        // newArrayCompareToIgnoreCase2 = rhs.data;
        char n, m;
        int value = 0;
        int loopSize;
        if (this.data.length < rhs.data.length) {
            loopSize = this.data.length;
        } else {
            loopSize = rhs.data.length;
        }
        for (int i = 0; i < loopSize; i++) {
            n = this.data[i];
            // char[] number = new char[1];
            if (n >= 97 && n <= 122) {
                n -= 32;
            }// if lower char change to capital char
            newArrayCompareToIgnoreCase[i] = n;
            m = rhs.data[i];
            if (m >= 97 && m <= 122) {
                m -= 32;
            }// if lower char change to capital char
            newArrayCompareToIgnoreCase2[i] = m;
            // by now everything should be lowercase
            for (int j = 0; j < loopSize; j++) {
                char char1 = newArrayCompareToIgnoreCase[j];
                char char2 = newArrayCompareToIgnoreCase2[j];
                if (char1 == char2) {
                    value = 0;
                }
                if (char1 != char2) {
                    value = char1 - char2;
                }
            }
        }
        return value;

    } // compares two string but casing douse not matter

    public CustomString concat(CustomString rhs) {
        char n, m;
        char[] newArrayConcat = new char[this.data.length + rhs.data.length + 2];
        for (int i = 0; i < this.data.length; i++) {
            n = this.data[i];
            // m = rhs.data[i];
            newArrayConcat[i] = n;
            // newArrayConcat[i+this.data.length]= m;
        }
        for (int j = 0; j < rhs.data.length; j++) {
            m = rhs.data[j];
            newArrayConcat[j + this.data.length] = m; // +1
        }
        return new CustomString(newArrayConcat); // change?

    }// returns a new CustomString object by concatenating source string and
        // parameter string
        // CustomString

    public boolean equals(CustomString rhs) {
        char[] newArrayEquals = new char[this.data.length];
        char[] newArrayEquals2 = new char[rhs.data.length];
        boolean equals = false;
        int length;
        char n, m;
        if (this.data.length > rhs.data.length) {
            length = rhs.data.length;
        } else {
            length = this.data.length;
        }
        for (int i = 0; i < length; i++) {
            n = this.data[i];
            m = rhs.data[i];
            newArrayEquals[i] = n;
            newArrayEquals2[i] = m;
        }
        for (int j = 0; j < length; j++) {
            char char1 = newArrayEquals[j];
            char char2 = newArrayEquals2[j];
            if (char1 != char2) {
                equals = false;
                break;
            } else if (char1 == char2) {
                equals = true;
            }

        }
        return equals;
    } // Returns true or false based on whether or not the two strings are
        // equal. NOTE: Does not ignore case

    public boolean equalsIgnoreCase(CustomString rhs) {
        int length;
        if (this.data.length > rhs.data.length) {
            length = rhs.data.length;
        } else {
            length = this.data.length;
        }
        char[] newArrayEqualsToIgnoreCase = new char[this.data.length];
        char[] newArrayEqualsToIgnoreCase2 = new char[rhs.data.length];
        char n, m;
        boolean equalsIgnoreCase = false;
        for (int i = 0; i < length; i++) { // must compare which string is
                                            // longer or else if
                                            // //newArrayEqualsToIgnoreCase.length
            n = this.data[i];
            m = rhs.data[i];
            if (n >= 65 && n <= 90) {
                n += 32;
            }
            if (m >= 65 && m <= 90) {
                m += 32;
            }// changes everything to lower case
            newArrayEqualsToIgnoreCase[i] = n;
            newArrayEqualsToIgnoreCase2[i] = m;
        }
        for (int j = 0; j < length; j++) { // this.data.length
            char char1 = newArrayEqualsToIgnoreCase[j];
            char char2 = newArrayEqualsToIgnoreCase2[j];
            if (char1 != char2) {
                equalsIgnoreCase = false;
                break;
            } else if (char1 == char2) {
                equalsIgnoreCase = true;
            }
        }
        return equalsIgnoreCase; // change?

    } // Same as equals but ignores the case

    public int length() {
        int charLength = this.data.length;
        return charLength; // change?

    } // Returns the length of the CustomString object

    public CustomString[] split(char delimiter) {
        // char n;
        char[] split = null;
        int numOfSplit = 1;
        int objectLocation=0;
        for (int h = 0; h < this.data.length; h++) {
            if (delimiter == this.data[h]) {
                numOfSplit++;
            }
        }

        CustomString[] splitter = new CustomString[numOfSplit];

        for (int i = 0; i < this.data.length; i++) {
            if (delimiter == this.data[i]) {
                // n = 32;//unicode for space}

                split = new char[i];
                for (int j = 0; j < split.length; j++) {
                    char z = this.data[j];
                    split[j] = z;
                    objectLocation++;

                }
            }

            splitter[objectLocation] = new CustomString(split);
        }
        return splitter;

    } // Returns a CustomString array, where each element of the array is a
        // CustomString object created by splitting the source string based
        // on the given char delimiter. This is an easier version of the split
        // method provided in the String class and
        // you only need to split on one character. The output array should NOT
        // contain the delimiter character.

    public boolean startsWith(CustomString prefix) {
        boolean startsWithEqual = false;
        char[] startsWith1 = new char[prefix.data.length];
        char[] startsWith2 = new char[prefix.data.length];
        char m, n;

        for (int i = 0; i < prefix.data.length; i++) {
            m = this.data[i];
            n = prefix.data[i];
            startsWith1[i] = m;
            startsWith2[i] = n;
        }
        for (int j = 0; j < prefix.data.length; j++) {
            if (startsWith1[j] == startsWith2[j]) {
                startsWithEqual = true;
            }
            if (startsWith1[j] != startsWith2[j]) {
                startsWithEqual = false;
                break;
            }
        }

        return startsWithEqual;
    } // Returns true if the source CustomString starts with the given prefix

    public boolean endsWith(CustomString suffix) {
        boolean endsWithEqual = false;
        char[] endsWith1 = new char[suffix.data.length];
        char[] endsWith2 = new char[suffix.data.length];
        char m, n;

        for (int i = 0; i < suffix.data.length; i++) {
            n = suffix.data[i];
            endsWith2[i] = n;
        }

        int k = 0;
        for (int i = this.data.length - suffix.data.length; i < this.data.length; i++) {
            m = this.data[i];
            endsWith1[k] = m;
            k++;
        }
        for (int j = 0; j < suffix.data.length; j++) {
            if (endsWith1[j] == endsWith2[j]) {
                endsWithEqual = true;
            }
            if (endsWith1[j] != endsWith2[j]) {
                endsWithEqual = false;
                break;
            }
        }

        return endsWithEqual;
    }

    // Returns true if the source CustomString contains the parameter

    public CustomString substring(int srcBegin) {
        char[] newArraySub1;
        if (srcBegin == 0) {
            newArraySub1 = new char[this.data.length - srcBegin];
        } else {
            newArraySub1 = new char[this.data.length - srcBegin + 1];
        }
        char n;
        for (int i = srcBegin; i < newArraySub1.length; i++) {
            n = this.data[i];// -1
            newArraySub1[i] = n;

        }
        return new CustomString(newArraySub1);

    } // Returns a new CustomString object created by finding the substring of
        // the source string starting
        // with src Begin and going to the end of the source string

    public CustomString substring(int srcBegin, int srcEnd) {
        char n;
        char[] newArraySub2 = new char[this.data.length - srcBegin];
        for (int i = srcBegin; i < srcEnd; i++) {
            n = this.data[i];
            newArraySub2[i] = n;
        }
        return new CustomString(newArraySub2);

    }// Returns a new CustomString object created by finding the substring of
        // the source starting with srcBegin and ending at srcEnd –

    public CustomString titleize() {
        char n, m;
        char[] titleizedChar = new char[this.data.length];
        for (int j = 0; j < this.data.length; j++) {
            m = this.data[j];
            if (m >= 65 && m <= 90) {
                m += 32;
                titleizedChar[j] = m;
            }

            for (int i = 0; i < 1; i++) {
                n = this.data[i];
                if (n >= 97 && n <= 122) {
                    n -= 32;
                }
                titleizedChar[i] = n;
            }

            titleizedChar[j] = m;
        }
        return new CustomString(titleizedChar); // change?

    } // Returns a new CustomString object where the first character or every
        // word is capitalized.

    public CustomString toLowerCase() {
        char[] toLowCase = new char[this.data.length];
        for (int i = 0; i < toLowCase.length; i++) {
            char n = this.data[i];
            if (n >= 65 && n <= 90) {
                n += 32;
            }
            toLowCase[i] = n;
        }

        return new CustomString(toLowCase);
    } // Returns a new CustomString object with all lower case letters.

    public CustomString toUpperCase() { // should be similar to lower case do
        char[] toUpperCase = new char[this.data.length];
        for (int i = 0; i < toUpperCase.length; i++) {
            char n = this.data[i];
            if (n >= 97 && n <= 122) {
                n -= 32;
            }
            toUpperCase[i] = n;
        }
        return new CustomString(toUpperCase);
    } // Returns a new CustomString object with all upper case letters.

    public String toString() {
        String toString1 = "";

        for (int i = 0; i < this.data.length; i++) {
            char n = this.data[i];
            toString1 += n;
        }
        return toString1;

    } // Returns a String representation of the CustomString object. This is the
        // only place where you are allowed to use a String variable to build
        // the output string and return it.

}// end of code

I'm trying to print each object inside the array but dont know how? Note: i already have a toString method.

Edit: I changed the main class from the original post at 10:45 pm i posted the wrong one

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
leftlopez
  • 63
  • 1
  • 3
  • 7

4 Answers4

0

You could use

java.util.Arrays.toString(blah.split(delim));

For this to work, your CustomString class would need to provide a toString() method, too.

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

You can use

System.out.println(new String(charArray))

or

Arrays.toString(charArray)

to convert character array into string for printing purpose. you can also refers to What does the output of a printed character array mean?

Instead of reading char 1 by 1 and append in the string. Simple use new String(data)

Community
  • 1
  • 1
Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
0

Unboxing Might help

try

System.out.println("Splitted String: " + blah.split(delim).toString());
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
AVIK DUTTA
  • 736
  • 6
  • 23
0

As per your question the blah.split(delim) is supposed to return an array of CustomString data type .

I am asuming that you have a class called CustomString ..

so in order to convert From CustomString type to normal string type you need to be specific about the CustomString .

Please mention the contents of the Class CustomString so that we could help you out

ok by the look of CustomString class it is storing an array of datatype char so but the problem is that the return type of split(delim) is an array of datatype CustomString so you will face problems while unboxing with tostring() .. give me a minute

AVIK DUTTA
  • 736
  • 6
  • 23