0

The following is the "main" body of work-doing code:

public class ArrayFunHouse
{
    //instance variables and constructors could be used, but are not really needed

    //getSum() will return the sum of the numbers from start to stop, not including stop
    public static int getSum(int[] numArray, int start, int stop)
    {
        int sum = 0;

        for(int count = start; count <= stop; count++) {
            sum += numArray[count];
        }
        return sum;
    }

    //getCount() will return number of times val is present
    public static int getCount(int[] numArray, int val)
    {
        int present = 0;

        for(int count = 0; count < numArray.length; count++) {
            if(numArray[count] == val) {
                present++;
            }
        }
        return present;
    }

    public static int[] removeVal(int[] numArray, int val)
    {
        int[] removal = new int[numArray.length - getCount(numArray, val)];
        int arbitrary = 0;

        for(int count = 0; count < removal.length; count++) {
            if(numArray[count] == val) {
                arbitrary++;
            } else {
                removal[count - arbitrary] = numArray[count];
            }
        }

        return removal;
    }
}

And the runner class:

import java.util.Arrays;

public class ArrayFunHouseRunner
{
public static void main( String args[] )
{
    int[] one = {4,10,0,1,7,6,5,3,2,9};

    System.out.println(Arrays.toString(one));
    System.out.println("sum of spots 3-6  =  " + ArrayFunHouse.getSum(one,3,6));
    System.out.println("sum of spots 2-9  =  " + ArrayFunHouse.getSum(one,2,9));
    System.out.println("# of 4s  =  " + ArrayFunHouse.getCount(one,4));
    System.out.println("# of 9s  =  " + ArrayFunHouse.getCount(one,9));
    System.out.println("# of 7s  =  " + ArrayFunHouse.getCount(one,7));
    System.out.println("new array with all 7s removed = "+     ArrayFunHouse.removeVal(one, 7));


}
}

Returns something that I would expect from attempting to print a class without a toString, i.e. this:

[4, 10, 0, 1, 7, 6, 5, 3, 2, 9]
sum of spots 3-6  =  19
sum of spots 2-9  =  33
number of 4s  =  1
number of 9s  =  1
number of 7s  =  1
new array with all 7s removed = [I@56f7ce53

I know that I called it correctly in the runner, there's not much there to screw up, can't pinpoint the issue, any advice?

Final edit: Well that's embarrassing. The issue was in fact in the runner, not calling an Arrays.toString.

4
  • 2
    How are you printing that value? Commented Nov 15, 2013 at 13:28
  • Show us the code where you're calling that method and using its return value. You may think you didn't screw that part up, but all the evidence points to the fact that you actually did. Commented Nov 15, 2013 at 13:28
  • 1
    Use Arrays.toString(array) Commented Nov 15, 2013 at 13:29
  • "Returns something that I would expect from attempting to print a class without a toString, i.e. this: [I@56f7ce53" - exactly. Arrays don't override toString. Commented Nov 15, 2013 at 13:29

2 Answers 2

4

print with

System.out.println(Arrays.toString(array));

and if your array has array inside.

System.out.println(Arrays.deepToString(array));
Sign up to request clarification or add additional context in comments.

Comments

1

As you stated, you are printing the toString() of the returned array. However, in Java, arrays don't override toString(), hence the output you're getting. Instead of just printing the returned value, use Arrays.toString(int[]) to print it's value - that should give you the desired output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.