Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to remove error using copyOf method?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting error in copyOf method . Can anyone tell where am i wrong ? I am getting error :
The method `copyOf(U[], int, Class<? extends T[]>)` in the type Arrays is not applicable for the arguments (Object[], int, Class<String>)
   Can only iterate over an array or an instance of `java.lang.Iterable

Can anyone please help removing this error ?

Error Logs :
 
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch I added code tags to your post; doesn't it look better now

Why have you got a static variable (the List) and why isn't it marked private? You have got too much code in the main method. It is intended only for starting an application, not to be the whole application.
You aren't closing the Scanner; find out how to close it with try with resources.
You have to do something about the exception declared by line 5.
Why don't you use the overloaded version of List#toArray(): String[] words = myList.toArray(new String[0]);
Have you read the documentation for Arrays#copyOf()? You have a U[], which you are using as the source, In which case U means Object. You are now trying to copy it into a String[], so you need some sort of class that extends T[] not T. Since String is a final class, the only class you can find to extend String[] is String[].class. That is what it means about Class<? extends T[]>. Whichever type of Class object you are passing has to represent something extending T[].
 
Master Rancher
Posts: 4700
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
You have to do something about the exception declared by line 5.



I think they are, with the FileNotFoundException?
Or is there another one that needs handling (I haven't tried the code out)?
 
Campbell Ritchie
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . the FileNotFoundException? . . .

That's what comes from trying th code on JShell; you forget the throws declaration
 
A wop bop a lu bop a womp bam boom! Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic