• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to compare and change similar strings from txt file?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!


How can I change similar strings taken from a txt file? For example:
"thisissame"
"thisissame"

Will become

"thisissame"
"thisissame2"

 
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch
Post here what you have got so far.
 
Mihkel Kass
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:Welcome to the ranch
Post here what you have got so far.


Thank you!

Code:



Example from text file:
Tõnis   Meel
Henri Külg
Anna  Tõnisson
Tom Niit
Tom Niit
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you can write each altered line to a new file and then rename the new file.

There is a lot more to say about your code, but I haven't enough time just at the moment.

And welcome to the Ranch again
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do the strings have to be next to each other in the file, or are you looking for them anywhere?
Is "Fred" considered the same as "fred"? What about "John Doe" and "John  Doe"?

My point is, before you write a single line of code, you should think through the problem more.  what makes strings "similar"?  Do you mean "identical"?  Do you need a new file with the updated info, or are you trying to edit the existing file? Why do you add "2" but not "1"?  Would a third duplicate become "thisissame22", or somoething else?

These details all matter. Generally, you should spend a lot more time thinking about the problem than writing code.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What that does is to take all the text out of the file and replace accented characters with unaccented characters. As Fred says, you will have to decide what you are going to do before you can work out how to do it.
Going backwards through your code, you have some repeated code. Lines 56‑59 shou‍ld be a separate method. Do you really need to replace ä twice?

Lines 41‑55 look confused; there are lots of double negatives in there. What is that supposed to do? Copy the characters and change them to lower case? It also contains a potentially dangerous bit of bad style:-
== false
Never use == true or == false, which are not only poor style but also error‑prone: what if you write = instead of ==? You get several errors for the price of one.
Not
if (b == true) ...
but
if (b) ...
Not
if (b == false) ...
but
if (!b) ...
The same applies to while. You have a nameDone variable and you are checking whether it is false. If you change the name of the variable, you can test whether it is true, and that will make code easier to read. Similarly, you can test
if (ch == space || ch == tab) ...
which removes all the negations, but you would have to swap the two halves of the if‑else. But it is not clear what those lines are doing. There must be a simpler way to do all that, if as Fred said, you think about what you want to do and plan it properly.

Why have you got so many static methods? Assume that the keyword static is an error unless you know a good reason for using it.

Why have you got so much code in the main method?

You are using a very old‑fashioned style. Why? Why use a buffered reader when you could use a Scanner? Why use a finally when you could use try with resources?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!