for loop on an array of strings Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of for loop on an array of strings without wasting too much if your time.

The question is published on by Tutorial Guruji team.

note: my if statement: if (newExteriorColor = availableExteriorColors) shouldn’t be used in this scenario therefor its incorrect so just wanted to clarify that i know its wrong

How can I make a for loop on the availableExteriorColors Array so the program will check if the selected color is equal to any String value in the array?

public class Cars {

    String [] availableExteriorColors = {"Black", "White", "Blue", "Red", "Yellow", "Brown"};

    public static String exteriorColor;
    public static String interiorColor;
    public static String carBrand;
    public static String carModel;
    public static String carPlateLetters;
    public static String carPlateNumber;
    public static int yearOfMake;
    //public static ? vinNumber; 

    public Cars(String newExteriorColor,String newInteriorColor,String newCarBrand,String newCarModel,String newCarPlateLetters,String newCarPlateNumber,int newYearOfMake)  
    {
        if (newExteriorColor = availableExteriorColors)
        {
            newExteriorColor = exteriorColors;
        } 
        else {
            System.out.println("Selected exterior color not available");
     return; }

Answer

for (String color : AvailableExteriorColors) {
    if (color.equals(selectedColor)) return color;
}
We are here to answer your question about for loop on an array of strings - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji