Search code examples
javaarraysabstract-classsubclassinstanceof

How to print a specific instance of an object using toString


How would I print and specific instance of an object using a toString?

So basically the user is inputing information. based on the input it will either saved in instance A or Instance B. Both instance A and B are subclasses with overriding toString methods. so the input from the user is saved in an array. How can I make it so that all the inputs that was an instance of A will print?

This is the code I currently have and it is not working.

public static void printA(ABC[] inputs)
    {
        for(int i = 0; i < inputs.length; i++)
        {   
            if(inputs[i] instanceof A)
            {
                JOptionPane.showMessageDialog(null, inputs.toString());
            }
        }
    }

Solution

  • you just need is

    JOptionPane.showMessageDialog(null, inputs[i].toString());
    

    cuz you are trying to show the array.toString() not the value you want to.