• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to return multiple strings of the same name from text file and array - any help

 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Another quick question. I have a text file with lines consisting of one integer, and four strings. Currently, I am able to search through this file and find any of the variables listed within. However, if any of the variables are listed more than once, and I search for that particular varable name, it only prints out the first line containing that particular variable name.

Basically, how do I modify the following code so that the search method returns all the instances of a particular variable name in the text file?

My current code is as follows,

1. Search method,

2. Test main class

Any help or advice appreciated!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said you wanted to return more than one Title. What's a nother word for "more than one" ... maybe a "collection" of Titles? You can return an array or List or whatever suits the task. Title[] is nice because it specifies the type better than List, but it's hard to create an array of the right size before you're done searching. In Java5 you can return List<Title>.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Many thanks for the reply.

I have tried implementing the lookUpDirector method as follows,

When I use this method with the previous test main class, it compiles and runs, but after the user inputs the name of the director to search for, the input window freezes indefinitely and no result is achieved. The output pane in NetBeans is as follows,

The only way to stop the process is to terminate it in the Runtime section of NetBeans.

I have no idea why this is happening. Any advice?
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your "next" variable, which you are using as an index, is only being incremented when it matches.... so... on you first mismatch, you will go into an endless loop checking the same title over and over again.

Henry
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. This problem is occurring even when the user searches for the name of a director, known to be in the text file currently being searched.

Why would this happen?

Many thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by celine scarlett:
Hi,

Thanks for the reply. This problem is occurring even when the user searches for the name of a director, known to be in the text file currently being searched.

Why would this happen?

Many thanks


Do you have more than one entry in the text file? And are they different? It will go into an endless loop as soon as it encounters a mismatch -- it doesn't matter if there is a match somewhere else.

Henry
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. I now see what you mean. If I enter the name of the director in the first line of the text file, it returns the correct details. Any search pas this line and the previous problem starts.

The text file being searched has many lines, each with one integer and four strings. Some lines may have the same string details.

With regard to my earlier code, however, how do I break it out of the continuous loop?

Many thanks!
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. It's all solved now.

I had been staring at it for too long, and couldn't see the wood for the trees. I have now moved the next statement outside the braces within the method, and it now returns the correct information from the file.

Have a great weekend!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic