• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading/outputting a text file with modified content  RSS feed

 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is sort of like the last problem I had but it's all about the formatting at the end. I have a file that reads something like:

Name
a bunch of text here
and perhaps a second
or even a third line

I need to output this as:
Name; "a bunch of text here and perhaps a second or even a third line"

Right now I find 'Name' and it outputs:
Namea bunch of text here and perhaps a second or even a third line

How would I add ";" and quotes in the middle? I can imagine that I may need to find name, then skip to the next line and just add name manually as the variable output of the search string.


Edit: Figured out part of it.

This is where I am now



I am able to print out everything pretty easily but I want skip the 'name' and just print the data under it.
My output looks like:

Name:""
Name:"info info info"
Name:"info info info
Name:"Name"
Name:"info info info"
Name:"info info info"
Name:"info info info"
Name:"info info info"
Name:"info info info"
Name:"info info info"
Name:"Name"
Name:"info info info"

So I'm almost there, but those dange double Names are killing me. I tried to add a bit of code where:

Hoping that if it found 'name' in the line it would just ignore it. Instead it ends up printing the name with a ton of space thrown in for fun. I'd really appreciate any help you guys can give.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:So I'm almost there, but those dange double Names are killing me. I tried to add a bit of code where...

And this, I suspect, is where you're having problems: You're too focused on how to code this, rather than analysing the problem.

For example: How do you know when you have a "name" in your input file? I still don't understand that part.

Second: Have you thought about creating a class that encapsulates a 'name' + 'stuff related to the name'? Java is an Object-Oriented language, so in general you want objects doing the work for you. What you have written so far is a procedure.

HIH

Winston
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 'name' is found here


The 'junk' is so I can ignore some unnecessary content.
So if output is true it prints the the line that it found 'name' exits the if statement and checks to see if the line is empty. If not it will print the next line and so on. I don't really understand what you're trying to hint at. I'd like it if I could maybe add something that would skip to the next line after finding 'name' but I haven't been able to find anything yet. Maybe, nextLine() or something but I don't know how to use it properly since we haven't been learning anything about scanner.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Figured it out:

This checks the line for the string 'name' then ignores it. Then it goes on to the next line. I had to use trim() in case some lines had spaces before the name. This is the only way I found, as a newbie, to avoid constantly creating new lines for each time it finds 'name'. I removed some formatting that was required to make this a more general solution to the problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!