i am having trouble with this code.

It does not let me compile in eclipse but there seems to be no error?

Any clue where my mistake lies?

thank you

package useToken;
import java.io.*;


class readFile extends StreamTokenizer {
	readFile(InputStream in ){
		super(in);
	}
	public static void main (String argv[]){
		try {
			int ret;
			String string;
			BufferedReader in =
				new BufferedReader (new InputStreamReader(System.in));
			
		
			System.out.print("Enter some text: ");
			string = in.readLine();
			
			
			String[] file = {"file1.txt","file2.txt","file3.txt"};
			for(int i=0;i<3;i++){
				
				
				FileInputStream fin = new FileInputStream(file[i]);
				readFile rt = new readFile(fin);
				
				
				int counter =0;
				while((ret = rt.nextToken())!= 0 && rt.sval != null){
					if(rt.sval.equals(string)){
						System.out.println("Found Text :" + rt.sval );
						counter++;
					}
				}
				System.out.println("The String Found :" + counter + " " + "times");
				System.out.println(file[i] + " " + "Complete");
			}
			
			
		} catch (Exception e ){
			System.err.println("Exception :" + e);
		}
	}
}

I do not know eclipse.
look at compile flag -Xlint:deprecation
or
change deprecated constructor

@Deprecatedpublic StreamTokenizer(InputStream is) to other:

Line 25 replace withFileInputStream is = new FileInputStream(file);
Reader r = new BufferedReader(new InputStreamReader(is));
ReadFile rt = new ReadFile(r);
where
ReadFile(Reader in) {
super(in);
}
Member Avatar for ztini

I was bored and this seemed like fun:

import java.io.BufferedReader;
import java.util.Scanner;


public class RepositorySearch {

	private String search;
	private String[] fileRepository = {
			"file1.txt", "file2.txt", "file3.txt" };
	private final String TAB = "     ";

	public RepositorySearch() {
		getInput();
		searchFiles();
	}
	
	private void getInput() {
		System.out.println();
		System.out.println(
		"   ,ad8888ba,                                          88              " + '\n' +
		"  d8\"'    `\"8b                                         88              " + '\n' +
		" d8'                                                   88              " +  '\n' +
		" 88              ,adPPYba,    ,adPPYba,    ,adPPYb,d8  88   ,adPPYba,  " +  '\n' +
		" 88      88888  a8\"     \"8a  a8\"     \"8a  a8\"    `Y88  88  a8P_____88  " +  '\n' +
		" Y8,        88  8b       d8  8b       d8  8b       88  88  8PP\"\"\"\"\"\"\"  " +  '\n' +
		"  Y8a.    .a88  \"8a,   ,a8\"  \"8a,   ,a8\"  \"8a,   ,d88  88  \"8b,   ,aa  " +  '\n' +
		"   `\"Y88888P\"    `\"YbbdP\"'    `\"YbbdP\"'    `\"YbbdP\"Y8  88   `\"Ybbd8\"'  " +  '\n' +
		"                                          aa,    ,88                  " +  '\n' +
		"                                           \"Y8bbdP\"                   ");
		
		System.out.println();
		System.out.println(TAB + TAB + "______________________________________________");
		System.out.print(TAB + TAB + "|  Search: ");
		search = new Scanner(System.in).nextLine();
		clearScreen();
	}
	
	private void clearScreen() {
		for (int i = 0; i < 65; i++) {
			System.out.println();
		}
	}
	
	private void searchFiles() {
		
		System.out.println(
			" __               |" + '\n' +
			"/__ _  _  _ | _   |       ______________________________________________" + '\n' +
			"\\_|(_)(_)(_||(/_  |      | Search : " + search + '\n' +
			"           _|     |");
		
		String tab = "                  | ";
		System.out.println(tab);
		System.out.println(tab);
		for (String fileName : fileRepository) {
			BufferedReader fileReader = Utility.File.open("src/" + fileName);
			
			int hitCounter = 0;
			while (Utility.File.hasNextLine(fileReader)) {
				String nextLine = Utility.File.nextLine(fileReader);
				if (nextLine.contains(search)) {
					System.out.println(tab + " " + nextLine);
					hitCounter++;
				}
			}
			
			if (hitCounter > 0) {
				System.out.println(tab + "   [" + fileName + ", " + hitCounter + " hit(s)]");
				System.out.println(tab);
				System.out.println(tab);
			}
			
			Utility.File.close(fileReader);	
		}
		
		System.out.println(tab);
		System.out.println(tab);		
	}
	
	public static void main(String[] args) {
		new RepositorySearch();
	}
	
}

Utility class:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class Utility {	
	static class File {
		public static BufferedReader open(String filename) {
			try { return new BufferedReader(new FileReader(filename)); }
			catch (IOException e) { return null; }
		}
		
		public static void close(BufferedReader reader) {
			try { reader.close(); }
			catch (IOException e) { } 
		}
		
		public static String nextLine(BufferedReader reader) {
			try { return reader.readLine(); } 
			catch (IOException e) { return null; }
		}
		
		public static boolean hasNextLine(BufferedReader reader) {
			try { return reader.ready(); } 
			catch (IOException e) { return false;}
		}
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.