0

Please take a look at this code (with ArrayList implementation)

public String returnCoreUnitsForProgram()
{
    ArrayList<String> testArray = new ArrayList<String>();

    ProgramClass pc = new ProgramClass("","","","");

    if ( pc.getProgramMajor().equals("Software Technology")
      || pc.getProgramMajor().equals("Network Technology" )
      || pc.getProgramMajor().equals("Games Technology" ) )
    {                           
       for(int i = 0; i< UnitArray.size() ; i++)
       {
            if (UnitArray.get(i).getUnitType().equals("Core"))
                testArray.add( UnitArray.get(i).getUnitName() );
       }                
    }  

    return testArray;
}

With ArrayList - I get the error "imcompatible types"

Without ArrayList:

public void returnCoreUnitsForProgram()
{               
    ProgramClass pc = new ProgramClass("","","","");

    if ( pc.getProgramMajor().equals("Software Technology")
      || pc.getProgramMajor().equals("Network Technology" )
      || pc.getProgramMajor().equals("Games Technology" ) )
    {                           
       for(int i = 0; i< UnitArray.size() ; i++)
       {
            if (UnitArray.get(i).getUnitType().equals("Core"))
            UnitArray.get(i).getUnitName();
       }                
    }                  
}

In another class "ProgramClass",

public String programToString()
{
    Apps apps = new Apps();
    String str = "Program Code: " + getProgramCode()
                +"\nProgram Code: " + getProgramName()
                +"\nProgram Major: "+ getProgramMajor()
                + apps.returnCoreUnitsForProgram();
    return str;
}

I can't possibly do this because returnCoreUnitsForProgram() method is void.

So I'm thinking that my only option is to use the first implementation method with ArrayList.

How can I solve this problem?

3
  • return a list of strings. Commented May 20, 2014 at 12:27
  • of course you have to use the arraylist or an array of strings Commented May 20, 2014 at 12:28
  • Read a book about java or programming! Commented May 20, 2014 at 12:32

4 Answers 4

6

You want to return an ArrayList, but you're returning a String. Change the function definition to

public ArrayList<String> returnCoreUnitsForProgram()

Also, you may want to do a lot more reading up on Java core concepts.

Sign up to request clarification or add additional context in comments.

Comments

1

In the top most example the method is declared to return a String, while you are attempting to return an ArrayList from the method body. May I suggest that you alter the declaration of the method to return List<String> instead, it should work (compile).

Thus;

public List<String> returnCoreUnitsForProgram(){

    ArrayList<String> testArray = new ArrayList<String>();

    ProgramClass pc = new ProgramClass("","","","");

    if ( pc.getProgramMajor().equals("Software Technology")
      || pc.getProgramMajor().equals("Network Technology" )
      || pc.getProgramMajor().equals("Games Technology" ) )
    {                           
       for(int i = 0; i< UnitArray.size() ; i++)
       {
            if (UnitArray.get(i).getUnitType().equals("Core"))
                testArray.add( UnitArray.get(i).getUnitName() );
       }                
    }  

    return testArray;
}

Comments

1

Try this with Arraylist implementation. Also in your programToString() method , iterate your list and append to String

 public List<String> returnCoreUnitsForProgram()
  {
   ArrayList<String> testArray = new ArrayList<String>();

    ProgramClass pc = new ProgramClass("","","","");

   if ( pc.getProgramMajor().equals("Software Technology")
  || pc.getProgramMajor().equals("Network Technology" )
  || pc.getProgramMajor().equals("Games Technology" ) )
 {                           
   for(int i = 0; i< UnitArray.size() ; i++)
   {
        if (UnitArray.get(i).getUnitType().equals("Core"))
            testArray.add( UnitArray.get(i).getUnitName() );
   }                
}  

return testArray;
}

Comments

0

Change method type from String to ArrayList<String> :)

Comments

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.