This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Help

 
Raymond Xu
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm an intro student to Java at UMass and I'm having trouble decoding Array methods. Any help would be appreciated!!

First Method is suppost to take an integer array as an input parameter and check to see if the array contains more zeros than non-zero values:

Second Method is suppost to take an integer array and return a new integer array in which all negative entries in the input array have been changed to zero:



EDIT by mw: Added Code Tags.
[ November 11, 2007: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

What problems are you having with this? How are you testing this?

Your code is very close to working. I would point out two things:
  • Your variables "count1" and "count2" are defined within the body of a for loop. They have no meaning outside of that scope.
  • You are returning the array reference correctly -- it's just not in the correct place. (Hint: You want to iterate through the entire array before returning.)
  •  
    Rusty Shackleford
    Ranch Hand
    Posts: 490
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Second Method is suppost to take an integer array and return a new integer array in which all negative entries in the input array have been changed to zero:
    public int[] negToZero (int[] theArray) {
    for(int j=0; j<theArray.length; j++){
    if(theArray[j]<0){
    theArray[j] = 0;
    }
    return theArray; //how do I return an Array??
    }
    }


    Well that method doesn't return a new array, but the old one, with negative values replaced with 0.

    That is exactly how you return an array.

    What specific questions do you have?
     
    Raymond Xu
    Greenhorn
    Posts: 8
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Darn, I should have caught those errors. Thank you all for the help!
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic