Console Based File Search Weird Results

Searching a Text file with a console program

Page 1 of 1

4 Replies - 1353 Views - Last Post: 04 March 2007 - 09:12 AM Rate Topic: -----

#1 codeninja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 16-February 07

Console Based File Search Weird Results

Posted 03 March 2007 - 11:08 AM

I am attaching a zip file with two files. Finder.java and test.txt .

I have a student who is using this code for a project. It is a console so it is not as modern as what you would use normally.

Anyway this code is adapted from two or three sources. I is supposed to search for key words and display any "line" that has that key word.

So if you search for "candle" you would get

candle 14
candle 15
candle 16

But the way it is set it displays

candle 14

candle 14

candle 14
candle 15

candle 14
candle 15
candle 16

The reason I am posting is that I understand why it is erroring but when I try and adjust the variables he has set -- input1-input2-input3

Or the IF statement

I can't get it to simply list all the lines in order without repeating.

I know it is easy it must be. But I have been working on it for 3 hours and keep missing something.

Any ideas would be helpful. I am going to help him move it from the console to a gui as soon as the algorithm is solved.

Codeninja

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Console Based File Search Weird Results

#2 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: Console Based File Search Weird Results

Posted 03 March 2007 - 12:37 PM

To be honest, I'm having trouble trying to understand what this program is supposed to do. I do see the error that you are talking about though. It is in the second if statement in the while loop.

 if ( line2.indexOf(wanted) >= 0 )  //the first if
 .
 .
 .
 if (line2.indexOf(wanted) >= 0)  //the second if



OK, I initially thought I knew what was wrong, but upon going back, I really don't see what you're trying to accomplish by having two of the same if statements in the same while loop. I do see that you change values in between, but again, I don't see what this is accomplishing.

One thing that I would suggest, however, is to check the values of line1 and line2 at the end of the while loop. Each time you circle through the while loop, you are changing the value in the variable line3, however, the values in variables line1 and line2 remain the same as they were at the end of the previous time through the while loop.

I wish I could be of more help. If you tell me exactly what you are looking for in terms of output

i.e. should it look like this:
[===context: 
candle14
===]


or this:
[===context: 
candle14

===]



I could probably help you a little more.

This post has been edited by keems21: 03 March 2007 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 codeninja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 16-February 07

Re: Console Based File Search Weird Results

Posted 04 March 2007 - 12:39 AM

I simply want to list any line that has the word CANDLE in it. And list it once.

I didn't write the code actually and I have modifid it four or five ways. I get different results.

The student -- to meet project criteria - has to develop a script that reads a text file either into an array and then sorts the array -- OR
can keyword search a text file.

It is like a litte DB.

His actual file looks like this

candle: 14 black 10 inch
candle:16 blue 8 inch
candle:13 red 5 inch

Basically it is a text file with a simple category system. The student has worked it out on his own, which I am happy about. BUT his alogorithm (which I am sure he has found online and modified) is just weird.

He is working on it as well.

I am going to give it another try later. But I have four projects right now all trying to meet the criteria in different ways. So I am being pulled left and right and when I jump from one project to the next I seem to overlook the simplest things.

I need more practice myself!

Any help is appreciated and I am also working on it. In the end I want him to move it to a GUI/JFrame and maybe use the Scanner instead of his current method.

-----------------Codeninja ----------------------

View Postkeems21, on 3 Mar, 2007 - 12:37 PM, said:

To be honest, I'm having trouble trying to understand what this program is supposed to do. I do see the error that you are talking about though. It is in the second if statement in the while loop.

 if ( line2.indexOf(wanted) >= 0 )  //the first if
 .
 .
 .
 if (line2.indexOf(wanted) >= 0)  //the second if



OK, I initially thought I knew what was wrong, but upon going back, I really don't see what you're trying to accomplish by having two of the same if statements in the same while loop. I do see that you change values in between, but again, I don't see what this is accomplishing.

One thing that I would suggest, however, is to check the values of line1 and line2 at the end of the while loop. Each time you circle through the while loop, you are changing the value in the variable line3, however, the values in variables line1 and line2 remain the same as they were at the end of the previous time through the while loop.

I wish I could be of more help. If you tell me exactly what you are looking for in terms of output

i.e. should it look like this:
[===context: 
candle14
===]


or this:
[===context: 
candle14

===]



I could probably help you a little more.

Was This Post Helpful? 0
  • +
  • -

#4 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: Console Based File Search Weird Results

Posted 04 March 2007 - 02:12 AM

OK, here's my second shot:

If I understand correctly, this should at least be close to what you're looking for.

import java.io.*;
import java.util.Scanner;

public class Finder2
{

	public static void main(String[] args) throws Exception
	{
		Scanner input = new Scanner(System.in);

		System.out.println("file name: ");
		String file = input.nextLine();
		System.out.println("word searching for: ");
		String word = input.nextLine();

		BufferedReader br = new BufferedReader(new FileReader(file));

		String line;

		int lineCounter = 0;
		while((line = br.readLine()) != null)
		{

			if(line.indexOf(word) != -1) //works because -1 is returned if the word is not located in the string
			{
				System.out.println("Word found on line " + lineCounter);
				System.out.println("Context: ");
				System.out.println(line);
			}

			lineCounter++;
		}
		input.close();
	}
}


The way that this is set up, the word being looked for is being searched for by the method indexOf(), which returns a positive value if it is found, but -1 if it is not. This is more reliable than trying to go through the String char by char, or with something like a tokenizer that will be thrown off by any punctuation.

The reason that this works and the other one doesn't is because it effectively uses the while loop. The needed variables are given values, then checked to see if the desired conditions are met. After this, the values are changed and the while loop cycles through again.

The major problem with the last program was that it was giving initial values to the variables, checking those variables for the desired conditions, changing the values of the variables, then checking them again. After the second check, the while loop cycled through again, and the same conditions were checked a second time in a row. Now, if the conditions were true the first time, then they would also be true the second time, therefore the values were printed twice in a row.

To illustrate this point to the student, you can show him how the code is interpreted through the cycles of the while loop.

For example:
x = 0;
while(x < 5)
{
  System.out.println("hello.  x = " + x);
  x++;
}



is the same as:
  x = 0;
  System.out.println("hello.  x = " + x);
  x++;
  System.out.println("hello.  x = " + x);
  x++;
  System.out.println("hello.  x = " + x);
  x++;
  System.out.println("hello.  x = " + x);
  x++;
  System.out.println("hello.  x = " + x);
  x++;



I hope that long winded explination makes sense. If not, I can try and explain it in another way.

edit:
so in conclusion, the student was using something like this as his while loop:
while(x < 5)
{
  System.out.println("hello.  x = " + x);
  x++;
  System.out.println("hello.  x = " + x);
}



Instead of the one above.

This post has been edited by keems21: 04 March 2007 - 02:14 AM

Was This Post Helpful? 0
  • +
  • -

#5 codeninja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 16-February 07

Re: Console Based File Search Weird Results

Posted 04 March 2007 - 09:12 AM

It makes alot of sense. And your method is easier. I should have tried that first considering that the results were obviously being repeated.

For whatever reason I was focusing on making the conditional -- smarter. I added alot to the if statement to try and force it to check more closely before it displayed anything.

Anyway thanks. As I said in another post as soon as the student is done with the whole project I will post it here in case someone else can use it. It should be well documented.

Codeninja
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1