aspose file tools*
The moose likes Beginning Java and the fly likes Array Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Help" Watch "Array Help" New topic
Author

Array Help

Raymond Xu
Greenhorn

Joined: Nov 11, 2007
Posts: 8
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

Joined: Aug 31, 2004
Posts: 11343

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.)


  • "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    Rusty Shackleford
    Ranch Hand

    Joined: Jan 03, 2006
    Posts: 490
    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?


    "Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
    Raymond Xu
    Greenhorn

    Joined: Nov 11, 2007
    Posts: 8
    Darn, I should have caught those errors. Thank you all for the help!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Array Help