-2

So I asked a question previously about returning arrays in Java, to which I got an answer, and now my code is updated with a getter, however I keep getting something along the lines of:

Phone@b08deb 

as an output, which is not what I want, and if I were to do something like:

return arr[1];

It tells me that Phone cannot be converted to Phone[], and I'm incredibly lost as to how to get simply the number "00000" to be spit out by the compiler from the array (as I do understand that I don't need to initialize the array because it defaults to 0)

Link to question: How do I return an array to another class in Java?

Updated code:

class TheDriverClass
{
    public static void main(String[] args)
    {
        Phone p = new Phone(5);
        System.out.println(p); 
// Here it's supposed to return the values of an array with size 5, 
//so it should print out 00000
     }
}

Next I have the class Phone:

class Phone
{
   private Phone[] arr; //I'm supposed to keep the array private
   public Phone(int theLength){
      arr = new Phone[size];
   }
   public Phone[] getPhone(){
      return arr;
   }
}
Grant Foster
  • 722
  • 2
  • 11
  • 21
captastic
  • 67
  • 9
  • Try looking at this question/answer: https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array – GBlodgett Oct 10 '18 at 18:20
  • 1
    Beyond that you'll need to write a `toString()` method in your `Phone` class – GBlodgett Oct 10 '18 at 18:20
  • What would you want to be printed out? The Phone class doesn't seem to contain any meaningful data. – Mick Mnemonic Oct 10 '18 at 18:20
  • @MickMnemonic since in the Driver class is giving the length of the array located in Phone class (Phone p = new Phone(5);), I want the Phone class to take that number and use it as the size of the array, then return the array like this: 00000 (or however many zeros as the length of the array) – captastic Oct 10 '18 at 18:24
  • You want an array of `Phone` objects or do you want a `Phone` with an array inside it. – Taylor Oct 10 '18 at 18:25
  • @Taylor whichever that can give me the output of "00000" – captastic Oct 10 '18 at 18:27
  • 1
    Um, how about `System.out.println("00000");` ? What are you trying to achieve? I'm not trying to be rude but it really seems like you don't understand the problem. – Taylor Oct 10 '18 at 18:29
  • @Taylor no, no, it's completely fine. I'm trying to get my array in Phone to take a number given by the driver class (in this case the number is 5), then return all the numbers of the array in a format where all the numbers are displayed like "00000" (or 3 zeros if Phone gave me the number 3, etc) – captastic Oct 10 '18 at 18:33

3 Answers3

1

When you System.out.println() an array it basically prints out the array pointer.

you need something like Arrays.toString(array) it will call the toString on each element and print out something that is human readable.

Also to have a Phone that you can read. You will need to override the toString() on it as well.

class Phone
{
   private int[] numbers; //I'm supposed to keep the array private
   public Phone(int theLength){
       numbers = new int[theLength];
   }
   public int[] getPhone(){
      return numbers;
   }

   @Override
   public String toString() {
       return Arrays.toString(numbers);// also can use  return Arrays.toString(numbers).replaceAll(",|\\[|\\]| ", ""); to clean up the , and []s
   }
}
Charles
  • 944
  • 5
  • 9
  • I'm not supposed to make any changes to my driver class if that's what you're implying that I do, and how would I override the toString()? – captastic Oct 10 '18 at 18:28
  • if you don't want the , and [] you can use return Arrays.toString(numbers).replaceAll(",|\\[|\\]| ", ""); – Charles Oct 10 '18 at 18:50
1
public class HelloWorld {

    public static void main(String[] args) {
        Phone p = new Phone(5);
        System.out.println(p.getPhone());
    }
}

class Phone {

  private int[] arr;

  public Phone(int size) {
    this.arr = new int[size];
  }

  /* convert array to string */
  public String getPhone() {
     return Arrays.toString(arr);
  }
}
  • If you would like to get the exact output as "00000" format, then you need to use StringBuilder
  • In the Phone class, the Phone[] arr will default to null, hence using int[] will default values to 0
Ajay V
  • 523
  • 5
  • 11
0

If you don't want to make any changes in Phone class then inherit Phone class in CustomPhone class and override toString. this will work when Phone class is not final. I guess the following code may solve your problem.

class PhoneCustom extends Phone{

public PhoneCustom(int theLength) {
    super(theLength);
}
@Override
public String toString() {
    StringBuffer sb = new StringBuffer();
    for(int i = 0; i < super.getPhone().length; i++) {      
        if(super.getPhone()[i] == null) {
            sb.append(0);
        }else {
            sb.append(super.getPhone()[i]);

        }

    }
    return sb.toString();
}
}

Also, modify toString according to your requirement in else part.

If you have flexiblity to modify you phone class then override the toString method in phone class.

class Phone
{
private Phone[] arr; //I'm supposed to keep the array private
public Phone(int theLength){
    arr = new Phone[theLength];
}
public Phone[] getPhone(){
    return arr;
}
@Override
public String toString() {
    StringBuffer sb = new StringBuffer();
    for(int i = 0; i < arr.length; i++) {       
        if(arr[i] == null) {
            sb.append(0);
        }else {
            sb.append(arr[i]);

        }

    }
    return sb.toString();
}
}
Pandey Amit
  • 657
  • 6
  • 19