1

I am working on creating a class that has a constructor that takes integers and creates an integer array, double array, and character array of the size of the integers. I would like to test my code as I go along in a driver class, but I cannot figure out how to call my method stealIntegerArray in my toString method. I understand that the code in the stealIntegerArray method is most likely very wrong, but I only need to know how to call on that method for now. I am getting the error "The method stealIntegerArray(int[]) in the type ArrayBoss is not applicable for the arguments ()".

public class ArrayBoss
{ 
 private int[] integerArray;
 private int intArray;
 public ArrayBoss(int i, int t, int c)
{
  intArray = i;
  int[] integerArray = new int[i];
}
public int[] stealIntegerArray(int[] 
numArray)
{
  numArray = integerArray;
  numArray[0] = -1234;
  return integerArray;
}
public String toString()
{
  return stealIntegerArray(); 
}
  • 1
    You never assign anything to `this.integerArray`. The `int[] integerArray` assigned in your constructor is lost. The `numArray` passed to `stealIntegerArray` is never used because you reassign to that name. And you can't `return stealIntegerArray()` because you want a `String`, but that returns a `int[]`, and that function also wants an argument but you provide none. – Siguza Nov 17 '20 at 21:22
  • @Siguza this is my first ever computer science course, I honestly don't know how to implement most of what you said. I am limited to what I can do in this program as it is only what we have learned so far, which is simply an introduction to using arrays. I need my toString method to output the array. How do I use this.integerArray? – Mike Farrell Nov 17 '20 at 21:28
  • 1
    You're not limited. But I suggest you read up on "variable scoping" in Java, as well as this: https://stackoverflow.com/q/40480/2302862 – Siguza Nov 17 '20 at 21:35
  • *I cannot figure out how to call my method stealIntegerArray in my toString method* - why do you think you need that? `toString()` is about providing a `String` representation of your object. While the task of `stealIntegerArray()` is a bit unclear, it has absolutely no `String` in it, and the implementation may easily be wrong, the it-contains-no-String part is probably fine. – tevemadar Nov 17 '20 at 21:35
  • @tevemadar I think this because returning a method in my `toString` method is the only thing I have learned so far. How would I go about returning a string? – Mike Farrell Nov 17 '20 at 21:42
  • 1
    `return "Test";` – tevemadar Nov 17 '20 at 21:46

1 Answers1

0

You are trying to call the stealIntegerArray() method in the toString() method without any arguments, but the definition of the stealIntegerArray() method expect an int[] array reference (or null) as an argument. Depending on what you want to do you can call it like:

stealIntegerArray(null);
stealIntegerArray(new int[10]);
stealIntegerArray(variableOfAnIntArray);
Progman
  • 16,827
  • 6
  • 33
  • 48
  • If I am inputting the elements in a driver class, what do I want to have in the `stealIntegerArray()` as a best option? – Mike Farrell Nov 17 '20 at 21:31
  • @MikeFarrell What is a driver class? You can use any array reference you want as an argument for the `stealIntegerArray()` call. You have to figure out for yourself what you want, we cannot know what you want. – Progman Nov 17 '20 at 21:35
  • It is what my Professor calls a class that uses another class, so I would have another separate class that is creating an ArrayBoss object and adding elements to each array I create – Mike Farrell Nov 17 '20 at 21:41