• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read next line string

 
Tai Yo
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am loading text file contents to GUI using this code:

Then contents of text.txt file:

On jTextField2 I am getting this line: Sound, mobility and landscapes of exhibition: radio-guided.
The problem is I don't know how to get to jTextField2 the string of next line "tours at the Science Museum".
I would like to ask how can I get both line on jTextField2: Sound, mobility and landscapes of exhibition: radio-guided tours at the Science Museum?
Thank you in advance for any help.
 
Rob Spoor
Sheriff
Pie
Posts: 20750
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would read the file not line by line, but group of lines by group of lines, where each group of lines starts with JOURNAL, TITLE, AUTHOR etc and ends when the next group is encountered.

In pseudo code:
 
Tai Yo
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob Thank you for reply. I cannot understand 3rd line, it must be like this:
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to check for "JOURNAL" or "TITLE" or "AUTHOR". Rather than a long complex if condition it might be better to extract it to a method so you can say something like
 
Tai Yo
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If possible could you give me some example or link for deeper understanding. I am new to this field. Thank you.
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need to do is think in detail about how you would solve the problem if you were given a piece of paper with the information on it and were told to copy each piece of data identified by either "JOURNAL" or "TITLE" or "AUTHOR" to another form.

One way of doing this would be to read each line on the information sheet looking for one of the known markers. Once a marker has been found read the rest of the text on that line and all subsequent lines until another marker is found. Then write the data read in onto the form in the field identified by that marker.

In code that means having a loop that reads in each line and looks for a group marker (ie "JOURNAL" or "TITLE" or "AUTHOR"). Once a new group marker is found all the data read in so far can be stored and referenced by the previous group marker (putting the data in a map with the group marker as the key could be a good way of storing it). Once the whole file has been read in the map will hold all the data broken into the relevant groupings and so you can easily extract the appropriate data for each group marker.
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware: This post contains random thoughts and uncontrolled ramblings of a deranged mind (mine) Read on at your own risk!


I am sure it is simple to set up a Stream which will group your lines by their first token into a Map<String, List<String>>. But it is very unfortunate that you have that file written for use on paper, i.e. with line breaks rather than line wrapping. So you end up with lines whose first token is not AUTHOR, TITLE nor JOURNAL. I would suggest you might have to copy that file into another, joining the lines together if they don't start AUTHOR, TITLE or JOURNAL. You should also verify that there are no other possible tokens like that beginning lines; you don't want your program to fall apart when you get a line like this:-
PAGES       123—147
And I hope the // comments do not appear in your file, otherwise you will have to remove them. String#split with "\\s+" and "//" as the delimiters may help. Remember you can use the second parameter for String#split(); if you pass 2, the resultant array will never have more than 2 elements.

A different approach would be to create an Article class which can append half lines to itself. Give the Article class an alreadyHasLine(String firstToken) method; when that returns true you know to move on to the next Article. If your Article class has a Map which can map AUTHOR→names etc, that may make it easier.
 
Tai Yo
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for no reply. For me takes time to implement and realize your answers. Will reply after getting results.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic