• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Storing words in an Array or ArrayList.

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
New homework assignment.  I know I created an ArrayList a while back.  I am copying from a past program into a new main argument, but I'm getting a couple of errors.  Here is my code, with the homework instructions (Comment lines).  I will paste in the error message from the output window of NetBeans.  Just need some advise how to output.



Here is the output error list:

java.lang.ClassFormatError: Method "<error>" in class wordup/WordUp has illegal signature "(Llist;)Ljava/lang/System$out$println;"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" C:\Users\The Vorpes\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
 
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like to me that you are trying to get the random element in an array instead of an ArrayList in the method getRandom. I'd make sure that you use ArrayList throughout instead of going from an ArrayList to an Array. In your default constructor you are also trying to add items to the ArrayList (not sure if that is your intention or not), there is absolutely nothing wrong with that. I just wanted to point that out.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message you posted doesn't seem to have anything to do with your code.

But look at line 42, where your comment says an error is reported. Which method is that line of code in? (Hint: it isn't)
 
David Vorpe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:The error message you posted doesn't seem to have anything to do with your code.

But look at line 42, where your comment says an error is reported. Which method is that line of code in? (Hint: it isn't)



Yeah.  I see that.  Happens to be a big ole, "DOH!" on my part.  (You all really need a Homer Simpson emoticon-I'll be using it a lot!)  I gotta be at work at 6:30am, so I will attack this again tomorrow.  Thank you for replying!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is why correct formatting and alignment of your program structures are important.

If you go through your program and match up each opening "{" with a closing "}"  you'll find that you're missing one closing "}"

Your code's indentation makes it difficult to see your program structure the way the compiler actually sees it.  On line 18, you start the main() method.  On line 20, you attempt to declare a public variable.  You can't declare public variables inside a method.  From that point on, any compiler errors you get won't really make a lot of sense.
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • You haven't closed main method.
  • I think you need not write index numbers whilst adding elements as ArrayList starts adding elements from index 0 and maintains insertion order unless you want to add a particular element at a specific index.
  • Although you close main method before ArrayList declaration print statement
  • will cause compile time error because you can't write print statement directly in class, you can write It only in body of contructor, method and blocks.

  • You defined method getRandom which accepts array of int as parameter which supposed to be generic ArrayList. I think you better continue using ArrayList rather than array in this method.
  • These methods might help you more in that case get(int index) and size()
  •  
    Marshal
    Posts: 79177
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ganesh Patekar wrote:[list]You haven't closed main method.
    . . .

    Are you sure? I think the problem is that OP is trying to put declarations and a constructor inside the main method; you must not use the keyword public inside a method. The solution is to move all that code out of the main method; none of it shou‍ld be in the main method at all.
    Don't declare fields as public; they shou‍ld all have private access. Only use the \n character if you have been instructed to use LF. In fact don't try putting any line end characters into your List. Not myList.add("Ferrari<lineEnd>"); but myList.add("Ferrari"); Use println or printf to print words on separate lines if you need to.
    Don't create the Random object as a local variable. Declare that Random object as a field and reuse the same object every time you need it. Why are you requiring an int[] as a parameter to that method? You don't have any int[]s anywhere.
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes you're correct, I wrongly assumed OP might have forgotten to put closing brace } of main method before ArrayList declaration because there is one less }
     
    Whatever. Here's a tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic