Skip to content
Advertisement

how to print company_name starting with letter a if the company_name function is getcompanyname()

public class YellowPages {
  //company array

  public Company[] save() {
    Company[] com = new Company[5];
    com[0] = new Company("abc", 123, "abc@gmail.com", 57839174, "abc.com", City.THRISSUR);
    com[1] = new Company("cde", 456, "cde@gmail.com", 47578317, "cde.com", City.TRIVANDRUM);
    com[2] = new Company("fgh", 789, "fgh@gmail.com", 82239129, "fgh.com", City.KOCHI);
    com[3] = new Company("ijk", 127, "ijk@gmail.com", 45758379, "ijk.com", City.KOZHIKODE);
    com[4] = new Company("lmn", 845, "lmn@gmail.com", 65893948, "lmn.com", City.KODUNGALLUR);

    return com;

  }
  //code to print all companies
  public void showData() {
    save();
    Company[] com = save();

    for (int i = 0; i < com.length; i++) {
      System.out.println(com[i].toString());
    }
  }
  //code to compare company_name

  public void comparecompany() {
    save();
    Company[] com = save();

    String a = "lmn";
    boolean found = false;
    for (int i = 0; i < com.length; i++) {
      String b = a.toLowerCase();

      if (com[i].getcompanyname().equals(b)) {
        System.out.println("company exist");
        found = true;
        break;
      }
    }
    if (found == false)
      System.out.println("company doesnt exist");

  }
  //code to find the string company_name which starts with a

  public void starta() {
    save();
    Company[] com = save();
    //show names containing the letter starting as the first letter

    for (int i = 0; i < com.length; i++) {
      String comp = com[i].getcompanyname();
      for (String i1: comp) {
        if (i1.startsWith("a")) System.out.println(i1);
      }
    }
  }
//to print employees in a company
public void empcompany() {
        save();
        Company[] com = save();
        saves();
        Employee[]emp=saves();
        String comp="abc";
        boolean found=false;
        for(int i=0;i<com.length;i++) {
            for(int j=0;j<emp.length;j++) {
            String coms=com[i].getcompanyname();
            String empd=emp[j].getempName();
            Company cos=emp[j].getcompany();
            if(com[i].getcompanyname().equals(empd)) {
                System.out.println(comp+" has "+empd+" as employees");
        }
            }
        }}
    //employee array
    public Employee[] saves() {
        save();
        Company[] com=save();
        Employee []emp=new Employee[5];
        emp[0]=new Employee(321,"rahul","manager","Male",907374383,City.KODUNGALLUR,com[0]);
        emp[1]=new Employee(654,"akhil","assistant manager","Male",703845983,City.KOZHIKODE,com[1]);
        emp[2]=new Employee(987,"mithun","supervisor","Male",598339834,City.KOCHI,com[2]);
        emp[3]=new Employee(721,"visakh","assistant supervisor","Male",339835300,City.TRIVANDRUM,com[3]);
        emp[4]=new Employee(548,"sharma","trainee","Male",545348945,City.THRISSUR,com[4]);
        return emp;
    }
    //to print all employees
    public void showDatas() {
        saves();
        Employee[] emp=saves();
        for(int i=0;i<emp.length;i++) {
            System.out.println(emp[i].toString());
        }
        
    }
}

the last function starta() should print the company_name starting with a.I have given com[i].getcompanynamefunction as a string to a variable comp.

Its should print the output as the companyname. this is the error shown

Exception in thread “main” java.lang.Error: Unresolved compilation problem: Can only iterate over an array or an instance of java.lang.Iterable

at com.aitrich.yellowpages.YellowPages.starta(YellowPages.java:53)
at com.aitrich.yellowpages.Main.main(Main.java:14)

Advertisement

Answer

This is happening because you are trying to run a for loop on a String, comp, where as a for loop is meant only for an array or an iterable.. Try with below code, it should work

package com.practice;

public class TestCompany {

    public static void main(String[] args) {
        new TestCompany().starta();

    }

    public void comparecompany() {
        Company[] com = save();

        String a = "lmn";
        boolean found = false;
        for (int i = 0; i < com.length; i++) {
            String b = a.toLowerCase();

            if (com[i].getcompanyName().equals(b)) {
                System.out.println("company exist");
                found = true;
                break;
            }
        }
        if (found == false)
            System.out.println("company doesnt exist");

    }
    // code to find the string company_name which starts with a

    private Company[] save() {
        Company[] companies = new Company[5];
        companies[0] = new Company("abc");
        companies[1] = new Company("def");
        companies[2] = new Company("ghi");
        companies[3] = new Company("jkl");
        companies[4] = new Company("ijk");
        return companies;

    }

    public void starta() {
        Company[] com = save();
        // show names containing the letter starting as the first letter

        for (int i = 0; i < com.length; i++) {
            String comp = (String) com[i].getcompanyName();
            if (comp.startsWith("a"))
                System.out.println(comp);
        }
    }
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement