473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Searching File for a given String using Command-line arg.

1
I'm to create a program that reads files through the command-line
using a for loop to process each of the file
(ie. search for string "out" in given files)
using scanner methods such as hasNextline() and nextLine etc.

the only thing i am having trouble with is the for loop.
how would i do this?

thanks in advance.
Feb 24 '08 #1
6 1981
r035198x
13,262 8TB
I'm to create a program that reads files through the command-line
using a for loop to process each of the file
(ie. search for string "out" in given files)
using scanner methods such as hasNextline() and nextLine etc.

the only thing i am having trouble with is the for loop.
how would i do this?

thanks in advance.
Post what you have done so far.
Feb 25 '08 #2
gaya3
184 100+
I'm to create a program that reads files through the command-line
using a for loop to process each of the file
(ie. search for string "out" in given files)
using scanner methods such as hasNextline() and nextLine etc.

the only thing i am having trouble with is the for loop.
how would i do this?

thanks in advance.


Hi....
what u have used is rt...
Following code may help u...
import java.util.Scanner;
import java.io.*;
class Parse
{
public static void main(String args[])
{
try{
Scanner sc = new Scanner(new File("C:/Data/Customer.txt"));
int count = 0;
while(sc.hasNext())
{
String result = (String)sc.next();
if(result.contains("for")){
count = count+1;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}


-Hamsa
Feb 26 '08 #3
JosAH
11,448 Expert 8TB
What is that command line supposed to contain? The names of the files to test?

kind regards,

Jos
Feb 26 '08 #4
gaya3
184 100+
What is that command line supposed to contain? The names of the files to test?

kind regards,

Jos
yes..

import java.util.Scanner;
import java.io.*;
class Parse
{
public static void main(String args[])
{
try{
Scanner sc = new Scanner(new File(args[0]));
int count = 0;
while(sc.hasNext())
{
String result = (String)sc.next();
if(result.contains("for")){
count = count+1;
}
}
System.out.println(count);
}catch(Exception e){
e.printStackTrace();
}
}
}


where args[0] is here Filename..

-Thanks & Regards
Hamsa
Mar 5 '08 #5
JosAH
11,448 Expert 8TB
yes..

import java.util.Scanner;
import java.io.*;
class Parse
{
public static void main(String args[])
{
try{
Scanner sc = new Scanner(new File(args[0]));
int count = 0;
while(sc.hasNext())
{
String result = (String)sc.next();
if(result.contains("for")){
count = count+1;
}
}
System.out.println(count);
}catch(Exception e){
e.printStackTrace();
}
}
}


where args[0] is here Filename..

-Thanks & Regards
Hamsa
You should loop through all the file names args[i] and process them. btw what do
you do with 'words' such as "forkformforum" when you search for the word "for"?

kind regards,

Jos
Mar 5 '08 #6
gaya3
184 100+
You should loop through all the file names args[i] and process them. btw what do
you do with 'words' such as "forkformforum" when you search for the word "for"?

kind regards,

Jos

Hi,
Thank u for correcting my code..
I have altered n now it works fine..

import java.util.Scanner;
import java.io.*;
class Parse{
public static void main(String args[]){
try{
Scanner sc = new Scanner(new File(args[0]));
int count = 0;
while(sc.hasNext()){
String result = (String)sc.next();
if(result.contains("for")){
for(int i=0;i<result.length();){
int j = i+3;
if(j<=result.length()){
if("for".equals(result.substring(i,i+3)))
count++;
}
i=i+3;
}
}
}
System.out.println("count:::::::::"+count);
}catch(Exception e){
e.printStackTrace();
}
}
}

-Thanks 7 Regards,
Hamsa
Mar 6 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Christopher R. Barry | last post by:
I need to search and replace multiple words in one pass of an input stream or string. For example, given the input: "The quick brown fox jumped over the lazy dog's back" and given the...
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
2
by: Shailesh Gajare | last post by:
Hi All, I have creating an ASP.Net application with two web servers. I am uploading a file which is being uploaded on one of the server, I want to copy the uploaded file on the other server at the...
5
by: David Wender | last post by:
I want to create a dataview with a sort on multiple columns. However, when I use FindRows, I only want to search some of the columns, not all. Is this possible? I have not been able to make it...
3
by: ciaran.mchale | last post by:
Hi folks, I downloaded the binary version of Xerces C++ 2.7.0 for Windows and am using it to help me get up to speed with XML and XML Schema. So please excuse me if this is a "novice" question....
7
by: pbd22 | last post by:
Hi. I am somewhat new to this and would like some advice. I want to search my xml file using "keyword" search and return results based on "proximity matching" - in other words, since the search...
4
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given...
3
by: Andu | last post by:
Ok, i'm using Linux Ubuntu, running on kernel 2.6.15-23 and i am writing a program in python that has to do with memory. I need to open the file System.map-2.6.15-23-386 which i managed to do. ...
2
by: neilio2j | last post by:
hi, can anyone tell me how to search a given directory using C++? so far i have been successful in searching a file named, for tags"h:\\tags.cpp", whereas i would like to search for all files...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.