0

I want to get the model numbers from the list only

['brand: Samsung, model number: VA2210-MH, size: 21.5', 'brand: Philipe, model number: 244E1SB, size: 21.5']

And I set create attributes and getter and setter of all attributes(only model number will be shown) in Monitor

public class Monitor{

    public String brand;
    public String modelNumber;
    public double size;


    public Monitor(String brand, String modelNumber, double size){
        this.brand = brand;
        this.modelNumber = modelNumber;
        this.size = size;
    }

    public void setModelNumber(String amodelNumber){
        modelNumber = amodelNumber;
    }

    public String getModelNumber(){
        return modelNumber;
    }
}

so I create a list and add the information into the list and a method to create a set with model number by the method modelNumberSet()

    import java.util.*; 

public class ComputerShop{


    private List<Monitor> monitorList = new ArrayList<>();


    public void addMonitor(String brand, String modelNumber, double size){
        Monitor newMonitor = new Monitor(brand, modelNumber, size);
        monitorList.add(newMonitor);
    }

    public Set<Monitor> modelNumberSet(){

        Set<Monitor> NewSet = new HashSet<>();


        for (Monitor m : monitorList) {
           NewSet.add(m.getModelNumber());
        }

        return NewSet;
    }
}

I hope the model number will be added into a new set, the output looks like

[VA2210-MH, 244E1SB]

So I use for loop to incase I will add more information in the future, but the error comes out when I use add(). Why the array cannot be added into the new set? Am I using the wrong function? Is there a better solution I should use?

2
  • Change Set<Monitor> to set<String> . You are adding model numbers to the set and their types are string. Commented Nov 24, 2019 at 17:06
  • Follow Java naming conventions. Variables such as NewSet should start with a lowercase letter, newSet. Even better would be using a meaningful name rather than generic “newSet”. Commented Nov 24, 2019 at 17:07

1 Answer 1

1

Change Set<Monitor> to Set<String>. You are adding model numbers to the set and their types are String. You are trying to put a String where a Monitor is expected. Square peg in a round hole.

Fix the modelNumberSet() method as follows:

    public Set<String> modelNumberSet(){
        Set<String> newSet = new HashSet<>();

        for (Monitor m : monitorList) {
            newSet.add(m.getModelNumber());
        }

        return newSet;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

than you very much
@PAKKINHON Sure, if this answers your question, please mark it as the correct answer.

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.