NoobestPros
NoobestPros

Reputation: 65

Creating own String class, system prints out null first

I am currently creating my own "String" class.

Here is my program... not going to put everything but at least the stuff that will help you test.

public class MyString {

private char [] string;
public int counter = 0;

public MyString(char [] chars)
{
    this.string = chars;
//I added this extra method here to show you the current stuff in char.(no null was there)
    try{
        for(int i = 0;; i++){
            System.out.print(string[i]);
            counter++;
        }
    }
    catch (Exception e){
        System.out.println();
    }
}



public MyString toLowerCase() {
    char [] temp = new char [counter];
    for(int i = 0; i < length(); i++)
    {
        if(string[i] < 91 && string[i] > 64)
            temp[i] = (char) (string[i] + 32);
        else
            temp[i] = string[i];
    }

    MyString s = new MyString(temp);

    return s; //prints the try method above again, no null
}
}

On a class with a main function...

public class temp {

public static void main(String[] args) {

char [] str2 = new char [5];
    str2[0] = 'H';
    str2[1] = 'E';
    str2[2] = 'l';
    str2[3] = 'l';
    str2[4] = 'O';

MyString s = new MyString(str2);

System.out.println(s.toLowerCase()); //null came out from here..
}
}

The output of this code is...

HEllO
hello
nullhello

As you can see, it started with a null. I was wondering what could have caused the problem like that. As you see I have added a try method on the constructor to test the array of chars. No null were there. Until I use it on the main, a null came out.

Upvotes: 2

Views: 173

Answers (3)

Ankit Deshpande
Ankit Deshpande

Reputation: 3604

You can use StringBuilder/StringBuffer to create string

public String toString() {
        StringBuilder sr = new StringBuilder();
        for (char x : string) {
            sr.append(x);
        }
        return sr.toString();
}

Upvotes: 0

Sar
Sar

Reputation: 17

Have you implemented the function length() in your toLowerCase function? Similarly the for loop, you can use string.length as the condition instead of leaving it blank.

try{
        for(int i = 0;; i++)
            System.out.print(string[i]);
    }

Instead, you can try directly print System.out.println(string)

or also,

try
{
     for(int i = 0;i<string.length(); i++)
         System.out.print(string[i]);
}

That may be the reason it gives null as your implementation goes beyond the limit of the string array, giving an exception.

Upvotes: 0

Eran
Eran

Reputation: 393831

System.out.println(s.toLowerCase());

would call your MyString class's toString method.

Based on your output, it probably looks like this (i.e. you are appending something to a null String) :

public String toString ()
{
    String result = null;
    for (int i = 0; i < string.length; i++)
        result += string[i];
    return result;
}

this would add a null at the start of the returned String.

A better toString method would be :

public String toString ()
{
    return new String(string);
}

Upvotes: 3

Related Questions