Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32
  1. #21
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default

    i want this method to receive (String arrays) and inside the body of this method i'll convert them to integers
    and then , the converted integers will be stored into new (int array) and return the (int array)

    i just want to know , how the prototype of the method should look like
    is it
    Java Code:
    public static String [] Convert ToString(String [] , String [])
    {
    ...
    ...
    return int [] x;
    }

    or


    Java Code:
    public static String [] Convert ToString(String [] , String [])
    {
    ...
    ...
    return int x;
    }
    Last edited by amrmb09; 11-20-2010 at 11:36 PM.

  2. #22
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You would know better than we would on whether you would want to return an int vs an int array, since only you know the full requirements of this method. But regardless, the method signature has to match what you return. If you are going to return an int array, you'd better have the method signature show this:

    Java Code:
    public static [color="red"][b]int[/b][/color] convertToString(String[] stringArray1, String[] stringArray2) {
      //... method body goes here
    }
    Also, the parameter variables will need names, here stringArray1 and stringArray2.


    What I do when creating new code like this is first make a method skeleton, something like:

    Java Code:
    public static int convertToString(String[] stringArray1, String[] stringArray2) {
      // TODO: finish
      return null;  // this will need to be changed so it will return an int array...
    }
    and just make sure that it compiles. I then modify the method stepwise and with each modification, check that it compiles before adding any new code.
    Last edited by Fubarable; 11-20-2010 at 11:44 PM.

  3. #23
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default

    but i want the method to return an integer array
    and you wrote

    Java Code:
    public static[COLOR="Red"] int [/COLOR]convertToString(String[] stringArray1, String[] stringArray2)

  4. #24
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by amrmb09 View Post
    but i want the method to return an integer array
    and you wrote

    Java Code:
    public static[COLOR="Red"] int [/COLOR]convertToString(String[] stringArray1, String[] stringArray2)
    I've been BUSTED! :eek:

    My bad -- you're right, it returns int[]

  5. #25
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I've been BUSTED! :eek:

    My bad -- you're right, it returns int[]
    aha, u should give me one of your reputation points then ;)
    anyway, and what about the return
    should it be
    Java Code:
    public static int [] convertStrToInt (String[] arr1 , String[] arr2 )
    {
    .....
    .....
    .....
    return int x[];
    }
    so that i can return the whole array of integers how??

  6. #26
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by amrmb09 View Post
    aha, u should give me one of your reputation points then ;)
    anyway, and what about the return
    should it be
    Java Code:
    public static int [] convertStrToInt (String[] arr1 , String[] arr2 )
    {
    .....
    .....
    .....
    return int x[];
    }
    so that i can return the whole array of integers
    You should return an array of int, but it won't look like that. You'll likely declare an int array in the method body, and will at the end return that variable but with the square brackets []:


    Java Code:
    public static int[] convertStrToInt (String[] arr1 , String[] arr2 )
    {
        int[] myIntArray = new int[?]; // ? you'll need some way to decide how big the array will be
    
        // methods here will fill the array
        return myIntArray; // then return the variable without the square brackets
    }

  7. #27
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default

    Java Code:
    many thanks Fubarable
    every thig is clear now

  8. #28
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're welcome and much luck. I also want to re-iterate that I didn't see Jos being angry, perhaps a little frustrated as we all were, but not angry. He's a smart coder -- I wish I knew half of what he knows, a great teacher, and a great forum resource. Just for what it's worth.

  9. #29
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Post

    I hope that the upcoming days could fix any misunderstanding occurred between him and me
    but he was so angry :)

  10. #30
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default

    hello, in this code, i would like to know how to pass the convertedWorkArray from the first method to the second one ??

    Java Code:
     public  int []convertStrToInt() 
        	{
        		int [] [COLOR="red"]convertedWorkArray [/COLOR]= new int [workarray.length];
        		    		
        		for (int i=0;i<workarray.length;i++)
        			convertedWorkArray[i]=Integer.parseInt(workarray[i]);
        		    System.out.println("Your array was converted into integers");
                return convertedWorkArray;
            } 
    
    public int getSum()
         {
         	int sum=0;
         	for (int i=0 ; i<[COLOR="red"]convertedWorkArray[/COLOR].length ; i++)
         		sum+ = convertedWorkArray[i];
         		return sum;               		
         }
    Last edited by amrmb09; 11-21-2010 at 02:35 AM.

  11. #31
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by amrmb09 View Post
    I hope that the upcoming days could fix any misunderstanding occurred between him and me
    but he was so angry :)
    I wasn't angry (why should I be?), just a little frustrated about all that unformatted code while Fubarable already had pointed you to a link explaining how to format your code; plus (the most important part), I'm too stupid to count to 36 in unformatted code and I'm too lazy to copy it all to vi and let vi do the counting.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #32
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    I wasn't angry (why should I be?), just a little frustrated about all that unformatted code while Fubarable already had pointed you to a link explaining how to format your code; plus (the most important part), I'm too stupid to count to 36 in unformatted code and I'm too lazy to copy it all to vi and let vi do the counting.

    kind regards,

    Jos
    hi Mr.Jos,

    good morning, I'm sorry about the misunderstanding from my side.

    Kind Regards,

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  2. Hello! and I need help. I dont know were to start
    By Fall0ut in forum New To Java
    Replies: 10
    Last Post: 05-19-2010, 06:26 PM
  3. Using libraries that others dont have(j3d)
    By scorpion9 in forum Advanced Java
    Replies: 7
    Last Post: 02-19-2010, 11:24 AM
  4. Sometimes get the right results sometimes dont
    By Battlefeldt in forum New To Java
    Replies: 0
    Last Post: 12-18-2009, 01:03 AM
  5. Replies: 6
    Last Post: 09-14-2009, 10:46 PM

Posting Permissions

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