This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing words in an Array or ArrayList.  RSS feed

 
David Vorpe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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)
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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()
  •  
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • 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 }
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!