The method toString() in the type Object is not applicable for the arguments (Collection)









up vote
-1
down vote

favorite












I'm new to Java and I am trying to write a description class that will return an array of strings from calling describe through my interface.



this line: return Collections.toString(items); is throwing the error in the title, and I can't understand why.



I know that I need to return type string and that items are not currently a string but I'm new to Java and not sure what to change.



trace error when run: java.lang.Error: Unresolved compilation problem:
The return type is incompatible with Describe.describe()



package uk.ac.uos.assignment;

import java.util.*;

public class Description implements Describe
private Collection<Describe> items;

public Description()
this.items = new ArrayList<>();


public String describe()
return Collections.toString(items);


public void add(Describe d)
items.add(d);




and this is my interface:



package uk.ac.uos.assignment;

interface Describe

String describe();










share|improve this question























  • Hi Brennan, can you please add the exception stack trace you are getting?
    – shriyog
    Nov 10 at 15:24










  • Collections has no static method toString, so the method you are trying to call in your describe function, does not really exist. If you call it as a Object function (return items.toString();), you will get rid of your error, but it isn't still doing what you want.
    – Axel M
    Nov 10 at 15:50











  • Please also add your Decribe Interface
    – Axel M
    Nov 10 at 16:07














up vote
-1
down vote

favorite












I'm new to Java and I am trying to write a description class that will return an array of strings from calling describe through my interface.



this line: return Collections.toString(items); is throwing the error in the title, and I can't understand why.



I know that I need to return type string and that items are not currently a string but I'm new to Java and not sure what to change.



trace error when run: java.lang.Error: Unresolved compilation problem:
The return type is incompatible with Describe.describe()



package uk.ac.uos.assignment;

import java.util.*;

public class Description implements Describe
private Collection<Describe> items;

public Description()
this.items = new ArrayList<>();


public String describe()
return Collections.toString(items);


public void add(Describe d)
items.add(d);




and this is my interface:



package uk.ac.uos.assignment;

interface Describe

String describe();










share|improve this question























  • Hi Brennan, can you please add the exception stack trace you are getting?
    – shriyog
    Nov 10 at 15:24










  • Collections has no static method toString, so the method you are trying to call in your describe function, does not really exist. If you call it as a Object function (return items.toString();), you will get rid of your error, but it isn't still doing what you want.
    – Axel M
    Nov 10 at 15:50











  • Please also add your Decribe Interface
    – Axel M
    Nov 10 at 16:07












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'm new to Java and I am trying to write a description class that will return an array of strings from calling describe through my interface.



this line: return Collections.toString(items); is throwing the error in the title, and I can't understand why.



I know that I need to return type string and that items are not currently a string but I'm new to Java and not sure what to change.



trace error when run: java.lang.Error: Unresolved compilation problem:
The return type is incompatible with Describe.describe()



package uk.ac.uos.assignment;

import java.util.*;

public class Description implements Describe
private Collection<Describe> items;

public Description()
this.items = new ArrayList<>();


public String describe()
return Collections.toString(items);


public void add(Describe d)
items.add(d);




and this is my interface:



package uk.ac.uos.assignment;

interface Describe

String describe();










share|improve this question















I'm new to Java and I am trying to write a description class that will return an array of strings from calling describe through my interface.



this line: return Collections.toString(items); is throwing the error in the title, and I can't understand why.



I know that I need to return type string and that items are not currently a string but I'm new to Java and not sure what to change.



trace error when run: java.lang.Error: Unresolved compilation problem:
The return type is incompatible with Describe.describe()



package uk.ac.uos.assignment;

import java.util.*;

public class Description implements Describe
private Collection<Describe> items;

public Description()
this.items = new ArrayList<>();


public String describe()
return Collections.toString(items);


public void add(Describe d)
items.add(d);




and this is my interface:



package uk.ac.uos.assignment;

interface Describe

String describe();







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 16:17

























asked Nov 10 at 15:12









Brennan Minns

12




12











  • Hi Brennan, can you please add the exception stack trace you are getting?
    – shriyog
    Nov 10 at 15:24










  • Collections has no static method toString, so the method you are trying to call in your describe function, does not really exist. If you call it as a Object function (return items.toString();), you will get rid of your error, but it isn't still doing what you want.
    – Axel M
    Nov 10 at 15:50











  • Please also add your Decribe Interface
    – Axel M
    Nov 10 at 16:07
















  • Hi Brennan, can you please add the exception stack trace you are getting?
    – shriyog
    Nov 10 at 15:24










  • Collections has no static method toString, so the method you are trying to call in your describe function, does not really exist. If you call it as a Object function (return items.toString();), you will get rid of your error, but it isn't still doing what you want.
    – Axel M
    Nov 10 at 15:50











  • Please also add your Decribe Interface
    – Axel M
    Nov 10 at 16:07















Hi Brennan, can you please add the exception stack trace you are getting?
– shriyog
Nov 10 at 15:24




Hi Brennan, can you please add the exception stack trace you are getting?
– shriyog
Nov 10 at 15:24












Collections has no static method toString, so the method you are trying to call in your describe function, does not really exist. If you call it as a Object function (return items.toString();), you will get rid of your error, but it isn't still doing what you want.
– Axel M
Nov 10 at 15:50





Collections has no static method toString, so the method you are trying to call in your describe function, does not really exist. If you call it as a Object function (return items.toString();), you will get rid of your error, but it isn't still doing what you want.
– Axel M
Nov 10 at 15:50













Please also add your Decribe Interface
– Axel M
Nov 10 at 16:07




Please also add your Decribe Interface
– Axel M
Nov 10 at 16:07












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Now, in the "describe()" method, I would suggest you do the following:



1) Create an empty String;



2) Iterate through the Collection and add every element, as a string, to the empty string you have created;



3) Return the String.



The basic algorithm I described above, now here's somewhat of an implementation:



public String describe()

StringBuilder y = new StringBuilder();
items.forEach(i -> y.append(i.toString()));

return y.toString();




Note: For this to work, your project must be set to use Java 8 or newer. If it is not, you will need to do a classical iteration through the Collection and then append each element to the StringBuilder.



Note 2: Your "Description" class must have its "toString()" method implemented. Before implementing it though, you should use an @Override annotation, just above the method:



@Override
public String toString()...





share|improve this answer





























    up vote
    0
    down vote













    What I understand is that you want a String array returning the String representation of each of those items in your Collection<Describe> items.



    Iterate over the collection and call the toString() method of every Describe item provided that the Describe class has its own implementation of toString.



    This would get you a String for each of those items in your collection, collect them and return at the end.



    public String describe() 
    List<String> descriptions = new ArrayList<String>(items.size());
    for(Describe item: items)
    descriptions.add(item.toString());

    return descriptions.toArray(new String[items.size()]);






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240285%2fthe-method-tostring-in-the-type-object-is-not-applicable-for-the-arguments-co%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Now, in the "describe()" method, I would suggest you do the following:



      1) Create an empty String;



      2) Iterate through the Collection and add every element, as a string, to the empty string you have created;



      3) Return the String.



      The basic algorithm I described above, now here's somewhat of an implementation:



      public String describe()

      StringBuilder y = new StringBuilder();
      items.forEach(i -> y.append(i.toString()));

      return y.toString();




      Note: For this to work, your project must be set to use Java 8 or newer. If it is not, you will need to do a classical iteration through the Collection and then append each element to the StringBuilder.



      Note 2: Your "Description" class must have its "toString()" method implemented. Before implementing it though, you should use an @Override annotation, just above the method:



      @Override
      public String toString()...





      share|improve this answer


























        up vote
        0
        down vote













        Now, in the "describe()" method, I would suggest you do the following:



        1) Create an empty String;



        2) Iterate through the Collection and add every element, as a string, to the empty string you have created;



        3) Return the String.



        The basic algorithm I described above, now here's somewhat of an implementation:



        public String describe()

        StringBuilder y = new StringBuilder();
        items.forEach(i -> y.append(i.toString()));

        return y.toString();




        Note: For this to work, your project must be set to use Java 8 or newer. If it is not, you will need to do a classical iteration through the Collection and then append each element to the StringBuilder.



        Note 2: Your "Description" class must have its "toString()" method implemented. Before implementing it though, you should use an @Override annotation, just above the method:



        @Override
        public String toString()...





        share|improve this answer
























          up vote
          0
          down vote










          up vote
          0
          down vote









          Now, in the "describe()" method, I would suggest you do the following:



          1) Create an empty String;



          2) Iterate through the Collection and add every element, as a string, to the empty string you have created;



          3) Return the String.



          The basic algorithm I described above, now here's somewhat of an implementation:



          public String describe()

          StringBuilder y = new StringBuilder();
          items.forEach(i -> y.append(i.toString()));

          return y.toString();




          Note: For this to work, your project must be set to use Java 8 or newer. If it is not, you will need to do a classical iteration through the Collection and then append each element to the StringBuilder.



          Note 2: Your "Description" class must have its "toString()" method implemented. Before implementing it though, you should use an @Override annotation, just above the method:



          @Override
          public String toString()...





          share|improve this answer














          Now, in the "describe()" method, I would suggest you do the following:



          1) Create an empty String;



          2) Iterate through the Collection and add every element, as a string, to the empty string you have created;



          3) Return the String.



          The basic algorithm I described above, now here's somewhat of an implementation:



          public String describe()

          StringBuilder y = new StringBuilder();
          items.forEach(i -> y.append(i.toString()));

          return y.toString();




          Note: For this to work, your project must be set to use Java 8 or newer. If it is not, you will need to do a classical iteration through the Collection and then append each element to the StringBuilder.



          Note 2: Your "Description" class must have its "toString()" method implemented. Before implementing it though, you should use an @Override annotation, just above the method:



          @Override
          public String toString()...






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 15:47

























          answered Nov 10 at 15:26









          Rakirnd

          714




          714






















              up vote
              0
              down vote













              What I understand is that you want a String array returning the String representation of each of those items in your Collection<Describe> items.



              Iterate over the collection and call the toString() method of every Describe item provided that the Describe class has its own implementation of toString.



              This would get you a String for each of those items in your collection, collect them and return at the end.



              public String describe() 
              List<String> descriptions = new ArrayList<String>(items.size());
              for(Describe item: items)
              descriptions.add(item.toString());

              return descriptions.toArray(new String[items.size()]);






              share|improve this answer


























                up vote
                0
                down vote













                What I understand is that you want a String array returning the String representation of each of those items in your Collection<Describe> items.



                Iterate over the collection and call the toString() method of every Describe item provided that the Describe class has its own implementation of toString.



                This would get you a String for each of those items in your collection, collect them and return at the end.



                public String describe() 
                List<String> descriptions = new ArrayList<String>(items.size());
                for(Describe item: items)
                descriptions.add(item.toString());

                return descriptions.toArray(new String[items.size()]);






                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  What I understand is that you want a String array returning the String representation of each of those items in your Collection<Describe> items.



                  Iterate over the collection and call the toString() method of every Describe item provided that the Describe class has its own implementation of toString.



                  This would get you a String for each of those items in your collection, collect them and return at the end.



                  public String describe() 
                  List<String> descriptions = new ArrayList<String>(items.size());
                  for(Describe item: items)
                  descriptions.add(item.toString());

                  return descriptions.toArray(new String[items.size()]);






                  share|improve this answer














                  What I understand is that you want a String array returning the String representation of each of those items in your Collection<Describe> items.



                  Iterate over the collection and call the toString() method of every Describe item provided that the Describe class has its own implementation of toString.



                  This would get you a String for each of those items in your collection, collect them and return at the end.



                  public String describe() 
                  List<String> descriptions = new ArrayList<String>(items.size());
                  for(Describe item: items)
                  descriptions.add(item.toString());

                  return descriptions.toArray(new String[items.size()]);







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 15:49

























                  answered Nov 10 at 15:15









                  shriyog

                  426616




                  426616



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240285%2fthe-method-tostring-in-the-type-object-is-not-applicable-for-the-arguments-co%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo