This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Help with Method Headers.

 
Greenhorn
Posts: 24
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following code:

I'm studying SCJP study guide and this is one of the questions i'm stuck on. I need to figure out what method header I can insert to make the program compile.

public static Collection<String> delete4LetterWords(List<String> myList)


That is one which I tried but it is still giving me error messages for at <E> and at the enhanced for statement.
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags next time or edit last post and use code tags.
change ..

to.

try this method header.


But there are also something wrong in your delete4LetterWords()
try to figure that out.
 
matthew meltzer
Greenhorn
Posts: 24
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply. the error I am getting now says "Can only iterate over an array or an instance of java.lang.Iterable"

here is my updated code.n



i'm still having a hard time figuring out what to fix in delete4letterword.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i am getting a different error

SoulSearching .java:16: foreach not applicable to expression type
for(E word: words){



 
matthew meltzer
Greenhorn
Posts: 24
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikhil Sagar wrote:But i am getting a different error

SoulSearching .java:16: foreach not applicable to expression type
for(E word: words){





Thats odd.

The error is showing up on the same line, so its referring to the same thing, just saying something different.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

matthew meltzer wrote:

Nikhil Sagar wrote:But i am getting a different error

SoulSearching .java:16: foreach not applicable to expression type
for(E word: words){





Thats odd.

The error is showing up on the same line, so its referring to the same thing, just saying something different.



Looks like we both are using different compilers.
Okay, Hi i am Nikhil Sagar and i am using sun's compiler.
Nice to meet you but excuse me it looks like that you are using some IDE and that IDE using a different compiler.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem here on this line.


is that the compiler changes E to List<String> and words is of type List<String> too.
and the for-each should be like.

But in your case the for loop is wrongly declared.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope you have figured it out.
Really very very interesting question, thanks for asking it here.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

matthew meltzer wrote:Thank you for your reply. the error I am getting now says "Can only iterate over an array or an instance of java.lang.Iterable"

here is my updated code.n



i'm still having a hard time figuring out what to fix in delete4letterword.



Why do you need to have "E words" in the formal param list?
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikhil Sagar wrote:Please use code tags next time or edit last post and use code tags.
change ..

to.

try this method header.


But there are also something wrong in your delete4LetterWords()
try to figure that out.



You don't need to introduce List<String> as the actual type of E. In the spirit of the question, you cannot change Collection<String> to Collection<List<String>>, it still needs to be String. With this hint, can you try again.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you cannot change Collection<String> to Collection<List<String>>


Looks like i changed as i said and its working.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Praveen Kumar M K wrote:

Nikhil Sagar wrote:Please use code tags next time or edit last post and use code tags.
change ..

to.

try this method header.


But there are also something wrong in your delete4LetterWords()
try to figure that out.



You don't need to introduce List<String> as the actual type of E. In the spirit of the question, you cannot change Collection<String> to Collection<List<String>>, it still needs to be String. With this hint, can you try again.



Do the same you posted and tell me if you can compile this line.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

matthew meltzer wrote: Given the following code:



Above is the original question. It clearly mentions "Given the following code", so you cannot change the code given in the question...instead you should find one of the options that would work for the Method Header.

Perhaps if OP can give the full question with the options(or quote the source) we can provide some help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!