2

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.

2
  • 5
    What does your toString method look like? Commented Sep 29, 2015 at 5:41
  • wow. okay that completely solved it LOL. my toString was initalized with null. and it added null along with the char of arrays Commented Sep 29, 2015 at 5:45

3 Answers 3

3
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);
}
Sign up to request clarification or add additional context in comments.

7 Comments

you are correct. the way how my toString method looks like. I was trying as hard as i can to avoid using any of the String Api methods.
That first example is interesting in that it does not cause a NPE, which is very confusing. String concatenation magic in the compiler...
Is it possible that it was initialized as String result = "null"; ??
@AnkitDeshpande It is possible, but the OP already commented that it was initialized to null.
i mean it could have avoided NPE if "null" as a string.. I'm getting this as output stringnull.MyString@2a139a55 not nullhello
|
0

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.

Comments

0

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.