Results 1 to 18 of 18
Thread: Static String Return Type
- 10-03-2009, 01:16 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Static String Return Type
Hello,
I have an array interface and I should write the methods for them,I have a question in the following method.
I should write a method String toString(int[] arr) that returns a string which, if printed, provides a niprint out of the array content. Used as:
int n = {3,4,5,6,7};
String str = Arrays.toString(n);
System.out.println("n = " + str);
I have written my method like this :
& the return type return NULL !I would like to know what I can I call as a return type.XML Code:static String toString(int[] arr) { System.out.print("\n"+ "All the values in the array are: " ); int i; String array = null; for ( i = 0; i < arr.length; i++ ) { array=Integer.toString(arr[i]); System.out.print(" "+ array); } return " "+ array; }
Thanks all in advance.Last edited by Java_Developer; 10-03-2009 at 01:20 PM.
- 10-03-2009, 01:40 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Di you call the method with array that has values.
- 10-03-2009, 01:57 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Yes Actually Like this
int[] array = new int[] {1,2,3,10,5};
Arrays.toString(array);
& It prints :
All the values in the array are: 1 2 3 10 5
Which is correct but It's the System.out.println that shows this line if I remove it wont show anything I mean the return " "+ array; won't return anything !
- 10-03-2009, 01:59 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
You do realise that this
results in only the last element being "saved" in the String, right?Java Code:array=Integer.toString(arr[i]);
- 10-03-2009, 02:05 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Yes you are right!But I couldn't find a better way to implement that method.
- 10-03-2009, 02:05 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 10-03-2009, 02:06 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 10-03-2009, 02:17 PM #8
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
& then I will get the following return type which is totally wrong :
All the values in the array are: null1 null12 null123 null12310 null123105
- 10-03-2009, 02:20 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Java Code:String array = ""; ... array += " " + Integ.... System.out.println(array);//this is obviously only debug
- 10-03-2009, 02:29 PM #10
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
You mean like this:
But still array won't return anything :(XML Code:static String toString(int[] arr) { System.out.print("\n"+ "All the values in the array are: " ); int i; String array = ""; for ( i = 0; i < arr.length; i++ ) { array+=" " + Integer.toString(arr[i]); } return array;
- 10-03-2009, 02:35 PM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Show the "calling" code. You probably doing something wrong outside of this method.
- 10-03-2009, 02:41 PM #12
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
The calling code is simple one :
int[] array = new int[] {1,2,3,10,5};
Arrays.toString(array);
- 10-03-2009, 03:19 PM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
That has nothing to do with the method you just wrote, and you are not storing the result of that anywhere.
- 10-03-2009, 03:39 PM #14
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Yes and the test is like :
String test=Arrays.toString(array);
System.out.print(test);
and it will show the last element in the array just 5 !
- 10-03-2009, 03:41 PM #15
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Read Masi's reply #9 above again.
- 10-03-2009, 03:49 PM #16
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
So, you've written your own class Arrays? You do know that there is already an Arrays class, right?
- 10-03-2009, 06:05 PM #17
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Yes I know and then testing this class with Junit .
- 10-03-2009, 06:07 PM #18
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Method return type problem
By McChill in forum New To JavaReplies: 7Last Post: 05-05-2009, 09:21 PM -
Static Method and Return Statements
By berelson in forum New To JavaReplies: 2Last Post: 11-29-2008, 11:17 PM -
[SOLVED] Cast string type to int type
By GilaMonster in forum New To JavaReplies: 9Last Post: 09-17-2008, 10:43 AM -
The return type
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 06:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks