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

what's wrong with my code?  RSS feed

 
fion bera
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I want to readLine all the text and remove the line in the text if there are 2 line with same string. then I want to printout the line after the redundancy is removed. I thought this few line of code should do it, but it printout nothing. I can't figure out which part of my code is wrong. Please help.
 
fion bera
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the right code.
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am surprised you got anything at all from the first code block. An Iterator should have the smallest possible scope; whenever you are “structurally modifying” a Collection, any Iterator in scope will probably notice and throw an Exception, as you will see in the documentation for HashSet.
 
fion bera
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first code, I can't get the element of the set to printout.
In the second code, I get it printout. It's because I put the iterator in the wrong place.
It's a NullUserException
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us the exact output from those two methods?
What’s a NullUserException? Do you mean NullPointerException? How do you get one of them from that code?
By the way, you should declare your Iterator as Iterator<String> and leave out the (String) cast. There is no need to cast anything to a String to print it out; the println method (indirectly) calls toString, which is an Object class method, so no casting is necessary.
 
fion bera
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the first one print out nothing. without any exception.
the second one print out all the element in the set.
p/s: NullUserException=no exception from the console. but the code is not doing what I want.(just a joke )
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you get the first code to run without a ConcurrentModificationException?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fion bera wrote:the second one print out all the element in the set.

Yes, but does it print it out in the right order? If order doesn't matter then it's probably fine; but if it does, I'd use a LinkedHashSet.

[Edit] Oh, and
Set set=new HashSet<String>();
should be
Set<String> set = new HashSet<String>();

Winston
 
fion bera
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:How do you get the first code to run without a ConcurrentModificationException?


I don't know. will I get an exception?
 
fion bera
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
fion bera wrote:the second one print out all the element in the set.

Yes, but does it print it out in the right order? If order doesn't matter then it's probably fine; but if it does, I'd use a LinkedHashSet.

Winston


it doesn't matter in my case. But thank for pointing that out
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the links I posted? The only way I can imagine that first code block running without an Exception is if it never adds anything to the Set.

Are you filling the set twice without emptying it? Find out what happens if you try to fill a set with the same elements twice.
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . Yes, but does it print it out in the right order? If order doesn't matter then it's probably fine . . .
A mathematician would say that sets do not support order, so not the right order is the right order.
 
fion bera
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have you read the links I posted? The only way I can imagine that first code block running without an Exception is if it never adds anything to the Set.


according to the documentation, I should get an exception, but I didn't.
 
fion bera
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Are you filling the set twice without emptying it? Find out what happens if you try to fill a set with the same elements twice.


I tried. no exception throws. nothing happen. the programme just continue
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the add() method and find its return type. You can use that to print out what the line is, and whether it has been added. To save time I shall show you:-The output will look bad if the lines are long. Your indentation is confusing; you have used a combination of tabs and spaces. Always use spaces.
 
fion bera
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Look at the add() method and find its return type. You can use that to print out what the line is, and whether it has been added. To save time I shall show you:-The output will look bad if the lines are long. Your indentation is confusing; you have used a combination of tabs and spaces. Always use spaces.



I see. Nothing is successfully added into set after the iterator is created. So, only can remove element from the set after the iterator is created, not add element
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does that mean? Which instance is adding anything and which isn’t. You have still not understood fail‑safe iterators. You can only alter the structure of the collection with the Iterator#remove() method.
 
fion bera
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What does that mean? Which instance is adding anything and which isn’t. You have still not understood fail‑safe iterators. You can only alter the structure of the collection with the Iterator#remove() method.


i have browser through alot of information, but can't understand what is fail-safe. But from my understanding. my code seem like fail-fast example that doesn't throws exception.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!