File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to return multiple strings of the same name from text file and array - any help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to return multiple strings of the same name from text file and array - any help" Watch "How to return multiple strings of the same name from text file and array - any help" New topic
Author

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

celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93
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

Joined: Jan 29, 2003
Posts: 8791
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>.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93
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
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93
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
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

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

Joined: Nov 06, 2005
Posts: 93
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

Joined: Nov 06, 2005
Posts: 93
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to return multiple strings of the same name from text file and array - any help