Results 1 to 18 of 18
  1. #1
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Question 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 :
    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;
    	}
    & the return type return NULL !I would like to know what I can I call as a return type.

    Thanks all in advance.
    Last edited by Java_Developer; 10-03-2009 at 01:20 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Di you call the method with array that has values.

  3. #3
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    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 !

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    You do realise that this
    Java Code:
    array=Integer.toString(arr[i]);
    results in only the last element being "saved" in the String, right?

  5. #5
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Question

    Yes you are right!But I couldn't find a better way to implement that method.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Java_Developer View Post
    ..
    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 !
    The println won't change the value that is returned by the method.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Quote Originally Posted by Java_Developer View Post
    Yes you are right!But I couldn't find a better way to implement that method.
    += maybe?

  8. #8
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    & then I will get the following return type which is totally wrong :

    All the values in the array are: null1 null12 null123 null12310 null123105

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Java Code:
    String array = "";
    ...
    array += " " + Integ....
    System.out.println(array);//this is obviously only debug

  10. #10
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Question

    You mean like this:

    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;
    But still array won't return anything :(

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Show the "calling" code. You probably doing something wrong outside of this method.

  12. #12
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    The calling code is simple one :

    int[] array = new int[] {1,2,3,10,5};
    Arrays.toString(array);

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    That has nothing to do with the method you just wrote, and you are not storing the result of that anywhere.

  14. #14
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    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 !

  15. #15
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read Masi's reply #9 above again.

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    So, you've written your own class Arrays? You do know that there is already an Arrays class, right?

  17. #17
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    Yes I know and then testing this class with Junit .

  18. #18
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Thumbs up

    Thanks for helping me,the problem solved now :)
    Last edited by Java_Developer; 10-03-2009 at 09:15 PM.

Similar Threads

  1. Method return type problem
    By McChill in forum New To Java
    Replies: 7
    Last Post: 05-05-2009, 09:21 PM
  2. Static Method and Return Statements
    By berelson in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 12:17 AM
  3. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  4. The return type
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •