The Artima Developer Community
Sponsored Link

Java Answers Forum
i want to check if a string exists in a file

2 replies on 1 page. Most recent reply: Sep 2, 2003 12:59 AM by swan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
aj3423 aj

Posts: 16
Nickname: aj3423
Registered: Aug, 2003

i want to check if a string exists in a file Posted: Aug 16, 2003 7:56 AM
Reply to this message Reply
Advertisement
i got a Exception when i run it :
D:\java>java Test
imput the filename : a.txt
imput the word to search : a

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Test.Check(Test.java:57)
at Test.main(Test.java:19)

Here is my code :

import java.io.* ;

public class Test {
public static void main(String args[]) throws IOException {
boolean flag = true;
while(flag) {
flag = false;
prt("imput the filename : ");
File Fname = new File(ImputString());
if (Fname.exists()) {
prt("imput the word to search : ");
String theWord = ImputString();
int length = theWord.length();
Check(Fname, ImputString(), length);
prt("the word not found .");
//Fname.close(); ,is this right ?
System.exit(0);
}else{
System.out.println("file not found : " + Fname);
prt("try again ?(y/n) : ");
BufferedReader YorN = new BufferedReader(new InputStreamReader(System.in));
String Choice = YorN.readLine();
if (Choice.toLowerCase().equals("y")) {
//check "y" or "n"
flag=true;
} else {
System.exit(0);
}
}
}
}

static String ImputString() throws IOException {
String str = null;
BufferedReader read = new BufferedReader(newInputStreamReader(System.in));
str = read.readLine();
return str;
}


static void Check(File theFile, String word, int length) throws IOException {
String theLine = null;
int i = 0;
int x = 0;
int m = 0;
int p = 0;
char Array[] = word.toCharArray();
FileInputStream fis = new FileInputStream(theFile);
DataInputStream in = new DataInputStream(new BufferedInputStream(fis));
while(in.available() != 0) {
while((char)in.read() == Array[p]) {
in.skip(1);
p++;
if(p == length) {
prt("found .");
fis.close();
System.exit(0);
}
}
}
}

static void prt(String s) {
System.out.print(s);
}
}


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: i want to check if a string exists in a file Posted: Sep 2, 2003 12:38 AM
Reply to this message Reply
Ok here is the code .
this takes two arguments
1 - string to find
2 - filename


import java.io.*;
 
class FindString
{
	public static void main(String[] args) throws IOException    
	{
		if(args.length != 2)
    	{ 
			System.out.println("Usage: FindString <string><filename>");
			System.exit(1);
    	}
		
		RandomAccessFile in = new RandomAccessFile(args[1],"r");
		String s = null;
 
		int i, loc = 0;
		for(i=1;(s = in.readLine()) != null;i++)
    	{
			loc = s.indexOf(args[0]);
			if (loc == -1)
				continue;	
			else
       		{
				System.out.println(args[0]+" found in line - "+i);				
				System.out.println(s);			
			}
		}
		
		System.out.println(args[0]+" found "+--i+"no of times in the file "+args[1]);
	}
}
 

swan

Posts: 7
Nickname: swan
Registered: Aug, 2003

Re: i want to check if a string exists in a file Posted: Sep 2, 2003 12:59 AM
Reply to this message Reply
Hi there,

Here is your code, modified, and the place where the mistake was committed.

import java.io.* ;

public class Test
{
public static void main(String args[]) throws IOException
{
boolean flag = true;
while(flag)
{
flag = false;
prt("imput the filename : ");
File Fname = new File(ImputString());
if (Fname.exists())
{
prt("imput the word to search : ");
String theWord = ImputString();
int length = theWord.length();
Check(Fname,theWord, length); // earlier it was ImputString() instead of theWord
prt("the word not found .");
//Fname.close(); ,is this right ?
System.exit(0);
}
else
{
System.out.println("file not found : " + Fname);
prt("try again ?(y/n) : ");
BufferedReader YorN = new BufferedReader(new InputStreamReader(System.in));
String Choice = YorN.readLine();
if (Choice.toLowerCase().equals("y"))
{
//check "y" or "n"
flag=true;
}
else
{
System.exit(0);
}
}
}
}

static String ImputString() throws IOException
{
String str = null;
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
str = read.readLine();
return str;
}


static void Check(File theFile, String word, int length) throws IOException
{
String theLine = null;
int i = 0;
int x = 0;
int m = 0;
int p = 0;

char Array[] = word.toCharArray();

FileInputStream fis = new FileInputStream(theFile);
DataInputStream in = new DataInputStream(new BufferedInputStream(fis));
while(in.available() != 0)
{
while((char)in.read() == Array[p])
{
in.skip(1);
p++;
if(p == length)
{
prt("found .");
fis.close();
System.exit(0);
}
}
}
}

static void prt(String s)
{
System.out.print(s);
}

}

Flat View: This topic has 2 replies on 1 page
Topic: Problem to display VRML file with Java3D and CyberVRML97 Previous Topic   Next Topic Topic: array index out of bounds

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use