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

Recommended Answers

All 3 Replies

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

Thanks a lot dear..

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.