Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Noob having trouble with weird exception  RSS feed

 
Olivier LeMaître
Greenhorn
Posts: 24
1
Firefox Browser Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am following the "Java headfirst" book and I was trying to complete chapter 6 on my own. However, when I run my code, it wil accept my input but after a couple of misses it throws an excetion.

This is the code:




And this is the exception:


I am (oubviously) a beginner, if you see that I made to many mistake or the code is just rubish, you can say so
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Javadocs for ArrayList:
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. 

So this seems to say that you need to use an iterator to loop through the List instead of an extended for-loop.  Give it a try and see if that works.
 
Olivier LeMaître
Greenhorn
Posts: 24
1
Firefox Browser Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thank you for your reply :-)
I have changed the "checkYourSelf" method:



but I still get this exception:
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need:
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oliver LeMaître wrote:
but I still get this exception:


You are not allowed to change the structure of the collection (ie. remove a element) while you are iterating through it. If you would like to remove() an element, while iterating, you must use the remove() method that is provided by the iterator.

Henry
 
Olivier LeMaître
Greenhorn
Posts: 24
1
Firefox Browser Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the code to:



But when I try to compile I get this error:
DotComBust.java:83: error: method remove in interface Iterator<E> cannot be applied to given types;
itera.remove(userInput);
     ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length
  where E is a type-variable:
    E extends Object declared in interface Iterator
1 error

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The iterator's remove() method does not takes a parameter. It removes the current element (meaning the last element that was returned by next() or previous()).

Henry
 
Olivier LeMaître
Greenhorn
Posts: 24
1
Firefox Browser Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henri,
I have only one problem left: the kill mechanic doesn't work.
First, It threw an exception, but when i changed the code to:

But whenever I am going to hit a ship a third time, it registers as a miss instead of a kill.
 
Olivier LeMaître
Greenhorn
Posts: 24
1
Firefox Browser Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oliver LeMaître wrote:


sorry, I ment:
 
Olivier LeMaître
Greenhorn
Posts: 24
1
Firefox Browser Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it works! I have changed some other code and it finally works! Thank you Henry and Knute!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!  Would you like to repost the code?
 
Olivier LeMaître
Greenhorn
Posts: 24
1
Firefox Browser Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my late reply, but here is the code:


hits and kills from the second and thirt dotCom object weren't shown, so I deleted the "break;" statement in the third "else if" statement in the "checkUserGuess" method that checked if it was a mis. But when I hit, for example, the second dotCom object, it printed out "miss" and on the next line "hit", so I added in a bit of code that only prints "miss" when all the Objects were checked. (Sorry for my bad English by the way, it's not my native language)
 
Dave Tolls
Rancher
Posts: 2911
35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oliver LeMaître wrote:Sorry for my late reply...


You call that a late reply?
Not even 24 hours?

We had someone post here recently 12 years later!

Amateurs...
;)
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no need to say "sorry".  Your English is better than a lot of people's.

Thanks for posting your code.  It looks great.  Do you mind if I make a few comments?

  • In getUserInput, you create a new BufferedReader each time the method is entered.  You can make it an instance variable and only initialize is once.
  • In the method placeDotCom, the array alphacoords doesn't seem to be used.
  • In line 152, while ( !success & attempts++ < 200 ), you probably want &&.
  •  
    Olivier LeMaître
    Greenhorn
    Posts: 24
    1
    Firefox Browser Linux Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry if I was not clear on this, but I didn't wrote the GameHelper class myself, it was part of the book, the code for that class was to complexe for my knowledge of the language.

    Knute Snortum wrote:
  • In getUserInput, you create a new BufferedReader each time the method is entered.  You can make it an instance variable and only initialize is once.



  • Does that mean that the variable is currently a "local variable" or am I wrong?

    Knute Snortum wrote:
  • In line 152, while ( !success & attempts++ < 200 ), you probably want &&.



  • I just looked up the diference and learned what "short-circuiting" means, is there any reason you want to use "&" or "|" to avoid short-circuit?


    ps: I would just want to say one last time how much I appreciate the support everybody gives and how nice the people are on this forum  
     
    Knute Snortum
    Sheriff
    Posts: 4073
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oliver LeMaître wrote:
    Knute Snortum wrote:
  • In getUserInput, you create a new BufferedReader each time the method is entered.  You can make it an instance variable and only initialize is once.



  • Does that mean that the variable is currently a "local variable" or am I wrong?

    That's correct


    Knute Snortum wrote:
  • In line 152, while ( !success & attempts++ < 200 ), you probably want &&.



  • I just looked up the diference and learned what "short-circuiting" means, is there any reason you want to use "&" or "|" to avoid short-circuit?

    It's not just short-circuiting.  "&" is a bit-wise operator.  That means it ANDs bits.  "&&" is a logical operator, meaning you can say condition1 && condition2 and both conditions have to be true to return true.
     
    Olivier LeMaître
    Greenhorn
    Posts: 24
    1
    Firefox Browser Linux Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's not just short-circuiting.  "&" is a bit-wise operator.  That means it ANDs bits.  "&&" is a logical operator, meaning you can say condition1 && condition2 and both conditions have to be true to return true.


    But when you are comparing two booleans, is there a reason you ever want to avoid short-circuiting?
     
    Knute Snortum
    Sheriff
    Posts: 4073
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not that I can think of.
     
    Campbell Ritchie
    Marshal
    Posts: 55681
    162
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You cannot use the short‑circuit if you want equality or exclusive or, but that is a separate problem.
     
    Paul Clapham
    Sheriff
    Posts: 22480
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Olivier LeMaître wrote:But when you are comparing two booleans, is there a reason you ever want to avoid short-circuiting?


    Consider this code:



    Here we have two boolean methods being tested. In this code if isReady() returns true then isAvailable() will not be called. That's clear already from this thread.

    But... what if calling isAvailable() has side effects, like updating a database? Granted a method with a name like isAvailable shouldn't have such side effects, but the possibility of such a design does exist (given the wide variations in design quality of real-life systems today). So far my example is pretty lame but there could be cases where such side effects are important in the operation of the application. Again this would be a pretty bad design but nevertheless it could be coded and implemented. And then a memo would have to be sent out reminding people to use & rather than && in such situations.

    Okay, it's pretty improbable. But the option is there to avoid short-circuiting and it's possible in some dystopian IT department somewhere it might become useful.
     
    Olivier LeMaître
    Greenhorn
    Posts: 24
    1
    Firefox Browser Linux Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    aaaaah yes, I understand. I overlooked the possibility that you can compare  functions that return a boolean. Thank you for pointing that out.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!