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.
toStringmethod look like?