Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Question about removing a : from a text file and printing

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to remove the : from a text file in notepad, and print it out in the console. I have read the javadocs on strings, but I think I am confused somewhere about the splitter, or how to implement it.

The text file is as follow.

ant:worker bee:busy scorpion:deadly ladybug:friendly spider:ugly

I want this output.

The ant is worker
The bee is busy
The scorpion is deadly
the ladybug is friendly
the spider is ugly

Here is what I have so far. I feel I am pretty close, but getting rid of that : character is tripping me up.

 
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem:line 9

The problem is line 9

See what I did there? You are splitting myAnimalString into its parts but in line 9 you never use the parts. Instead you still print out the whole myAnimalString.

Also, when using printf(), use %n to output a newline instead of \n
 
Jonathan Day
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:problem:line 9

The problem is line 9

See what I did there? You are splitting myAnimalString into its parts but in line 9 you never use the parts. Instead you still print out the whole myAnimalString.

Also, when using printf(), use %n to output a newline instead of \n



Thanks for the nudge in the right direction without giving it away. I do like to figure these things out on my own, but sometimes you just get stuck on something super simple. Glad I had it mostly right.

Took me a minute to think that out, and after I did there was still a problem, but... I got it.

Can't believe how close I was.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jonathan Day wrote:Is there a closed button?


I have marked this topic as resolved
 
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not convinced this question is quite closed.
For one thing, you are not closing your Scanner. You must always close Scanners reading files, sockets, etc. You need not close a Scanner reading a String directly, and you shouldn't close a Scanner reading System.in. Because you need to close the Scanner, it is better to make it a local variable rather than a field. If you look in the Java™ Tutorials, you will find that line 3 will definitely close the file without my writing close(); anywhere.Line 8 depends on the structure of your file and will throw exceptions, or omit data, if you don't have the right number of tokens.

Junilu: your turn. Explain what I have done wrong writing that method and show how it should be refactored.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Junilu: your turn.


I didn't realize we're tag-teaming these now...

I marked the thread resolved because OP's main problem has been addressed. However, since you pointed out some things and offered up some code for refactoring, well, I'm your huckleberry.

The first thing that jumps out at me is that the input source (Scanner) and the process (splitting and printing) are tightly bound together. This makes testing difficult. It would be much easier to test the processing logic if it were decoupled from the input source. So, I'd look into using Files.lines() to get a Stream<String> and keep that logic separate from the parsing and printing. I might even separate the parsing and printing logic as well.

I'll leave it to the interested to try to code that out the way I described it. Or even refactor the code given so that it becomes what I described.
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, wouldn't you want to separate the reading and printing?
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Also, wouldn't you want to separate the reading and printing?


I wrote:... I might even separate the parsing and printing logic as well.

 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for missing that bit.
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!