i need to return the ASCII value of the argument passed in the StringtoIntfunc(). But i am not getting the real ASCII values and i think the problem is with the return statements? please help.

public class StringIntfc
{
public int[] StringtoIntfunc(String s)
{
int j =0;
int k[] = new int[20];
for(int i=0;i<s.length();++i)
{
char c = s.charAt( i );
j = (int) c;
k = j;
}
return k;
}
}


public class StringIntTest
{
public static void main(String[] ar)
{
int ret[] = new int[4];
StringIntfc a = new StringIntfc();
ret = a.StringtoIntfunc("123");
System.out.println("return value is:" + ret);
}
}


The output of this program is: [I@19821f
But the actual output should be 495051

i need to return the ASCII value of the argument passed in the StringtoIntfunc(). But i am not getting the real ASCII values and i think the problem is with the return statements? please help.

public class StringIntfc
{
public int[] StringtoIntfunc(String s)
{
int j =0;
int k[] = new int[20];
for(int i=0;i<s.length();++i)
{
char c = s.charAt( i );
j = (int) c;
k = j;
}
return k;
}
}


public class StringIntTest
{
public static void main(String[] ar)
{
int ret[] = new int[4];
StringIntfc a = new StringIntfc();
ret = a.StringtoIntfunc("123");
System.out.println("return value is:" + ret);
}
}


The output of this program is: [I@19821f
But the actual output should be 495051

The problem is you're trying to ouput the entire integer array object hence the funky looking name.. try rather:

for(int i=0;i<ret.length;i++)
            if(ret[i]!=0)//not show unintialized values of array
System.out.print(ret[i]+",");

This will output each value stored in the arrays index... output should be 49,50,51, ofcourse you can make it change to your needs.
So no the problem is not your return statement, its the way you are printing the array, you must iterate through each index, not print the objects name

Edited 4 Years Ago by DavidKroukamp: n/a

Thanks a lot dear..

Glad its fixed, Please could you mark this thread as solved if it is, so other members may know this and the Daniweb archive may grow Thanks.

BTW to mark as solved go to the bottom of this page and click the Mark this thread as solved.

This question has already been answered. Start a new discussion instead.