aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help with Method Headers. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help with Method Headers." Watch "Help with Method Headers." New topic
Author

Help with Method Headers.

matthew meltzer
Greenhorn

Joined: Sep 08, 2012
Posts: 24

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.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

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.


OCPJP 6 86%
matthew meltzer
Greenhorn

Joined: Sep 08, 2012
Posts: 24

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

Joined: Apr 21, 2012
Posts: 216

But i am getting a different error
SoulSearching .java:16: foreach not applicable to expression type
for(E word: words){


matthew meltzer
Greenhorn

Joined: Sep 08, 2012
Posts: 24

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

Joined: Apr 21, 2012
Posts: 216

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

Joined: Apr 21, 2012
Posts: 216

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

Joined: Apr 21, 2012
Posts: 216

Hope you have figured it out.
Really very very interesting question, thanks for asking it here.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
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

Joined: Jul 03, 2011
Posts: 256
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

Joined: Apr 21, 2012
Posts: 216

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

Looks like i changed as i said and its working.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

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

Joined: Jul 03, 2011
Posts: 256
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Method Headers.
 
Similar Threads
scjp 1.5 question related to collections
Collections and Generics Help
Generics problem
Ultimate generics Q
Generics warnings... I don't understand?!