Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > hi

Reply
 
 
kavithadayalanvit@gmail.com
Guest
Posts: n/a
 
      01-31-2007
i want to search a particular word in the file using java.
can u give any specific link for this

regards
kavitha

 
Reply With Quote
 
 
 
 
Luc The Perverse
Guest
Posts: n/a
 
      01-31-2007
<> wrote in message
news: ups.com...
>i want to search a particular word in the file using java.
> can u give any specific link for this
>
> regards
> kavitha
>


Read the file

Check the input for the word

If the word is found then display something

Repeat.

. . . .

Maybe it would be better if you showed us what you had tried, specifically
what is not working and we can work from there.

--
LTP




 
Reply With Quote
 
 
 
 
Lab.Bhattacharjee@gmail.com
Guest
Posts: n/a
 
      01-31-2007
Hi kavitha,
u can try out the following code , it allows wild cards ,but is case
sensitive.Please tell me if u need that too??
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RnD {

public static void main(String[] args) {
Pattern pattern = Pattern.compile("SAXELBY");
BufferedReader reader = null;
try {
int lineNumber=0;
reader =
new BufferedReader(
new FileReader("C:\\00000001.txt"));
while (true) {
lineNumber++;
String curline = reader.readLine();
if (curline == null) {
break;
}
Matcher matcher = pattern.matcher(curline);
while (matcher.find()) {
System.out.println("at "+lineNumber+" "+
matcher.group()
+ " start="
+ matcher.start()
+ " end= "
+ matcher.end());
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

}
}

thanks
lab

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-31-2007
wrote:
> Hi kavitha,
> u can try out the following code , it allows wild cards ,but is case
> sensitive.Please tell me if u need that too??


The word is "you", not "u".

The trouble with doing someone's homework for them is that you deprive them of
an education. That's a big waste of their tuition.

> import java.io.BufferedReader;
> import java.io.FileNotFoundException;
> import java.io.FileReader;
> import java.io.IOException;
> import java.util.regex.Matcher;
> import java.util.regex.Pattern;
>
> public class RnD {
>
> public static void main(String[] args) {


Please do not embed TABs in Usenet posts.

> Pattern pattern = Pattern.compile("SAXELBY");


What is this regex? How did you come up with it? There was no indication in
the OP for this.

> BufferedReader reader = null;
> try {
> int lineNumber=0;
> reader =
> new BufferedReader(
> new FileReader("C:\\00000001.txt"));


Hard-coded strings. Tsk, tsk. Where did you come up with it? There was no
indication in the OP for this.

> while (true) {


To depend on exceptions as flow control is a bad practice.

> lineNumber++;
> String curline = reader.readLine();
> if (curline == null) {
> break;
> }
> Matcher matcher = pattern.matcher(curline);
> while (matcher.find()) {
> System.out.println("at "+lineNumber+" "+
> matcher.group()
> + " start="
> + matcher.start()
> + " end= "
> + matcher.end());
> }
> }
>
> } catch (FileNotFoundException e) {
> e.printStackTrace();


Ooh, you ignore the exception and move on. This is good for examples, bad in
practice.

This is also a good place to introduce logging statements.

> } catch (IOException e) {
> e.printStackTrace();
> } finally {
> if (reader != null) {
> try {
> reader.close();
> } catch (IOException e1) {
> e1.printStackTrace();


Those darn TABs.

> }
> }
> }
>
> }
> }


- Lew
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      01-31-2007
<> wrote in message
news: ups.com...
>i want to search a particular word in the file using java.
> can u give any specific link for this


http://www.google.ca/search?q=search...ile+using+java

- Oliver


 
Reply With Quote
 
Randolf Richardson
Guest
Posts: n/a
 
      02-02-2007
On Wed, 31 Jan 2007 04:55:35 -0800, Lew <> wrote:
> wrote:

[sNip]
>> while (true) {

>
> To depend on exceptions as flow control is a bad practice.

[sNip]

This typically occurs because the reserved word "goto" isn't
implemented. There are situations where "goto" would be very useful, such
as:

0. An alternative to "break label" since label is currently limited in
where it can be located (such code could be easier to read)

1. The ability to share code between methods within a class, which all
end with the same functionality (this could be more efficient than calling
another method; javac would need to generate errors such as attempts to
access variables that belong to different methods, return type mismatches,
etc.)

I do agree with your view that infinite loops that depend on exceptions
are a bad practice. Using conditionals to trigger a "break" would also be
better handled by making that the focus of the loop -- and if they need to
compare afterwards, then "do { ... } while (condition);" can certainly
solve that problem.

--
Randolf Richardson - kingpin+
The Lumber Cartel, local 42 (Canadian branch)
http://www.lumbercartel.ca/
 
Reply With Quote
 
kavithadayalanvit@gmail.com
Guest
Posts: n/a
 
      02-02-2007
On Jan 31, 5:11 pm, "Lab.Bhattachar...@gmail.com"
<Lab.Bhattachar...@gmail.com> wrote:
> Hi kavitha,
> u can try out the following code , it allows wild cards ,but is case
> sensitive.Please tell me if u need that too??
> import java.io.BufferedReader;
> import java.io.FileNotFoundException;
> import java.io.FileReader;
> import java.io.IOException;
> import java.util.regex.Matcher;
> import java.util.regex.Pattern;
>
> public class RnD {
>
> public static void main(String[] args) {
> Pattern pattern = Pattern.compile("SAXELBY");
> BufferedReader reader = null;
> try {
> int lineNumber=0;
> reader =
> new BufferedReader(
> new FileReader("C:\\00000001.txt"));
> while (true) {
> lineNumber++;
> String curline = reader.readLine();
> if (curline == null) {
> break;
> }
> Matcher matcher = pattern.matcher(curline);
> while (matcher.find()) {
> System.out.println("at "+lineNumber+" "+
> matcher.group()
> + " start="
> + matcher.start()
> + " end= "
> + matcher.end());
> }
> }
>
> } catch (FileNotFoundException e) {
> e.printStackTrace();
> } catch (IOException e) {
> e.printStackTrace();
> } finally {
> if (reader != null) {
> try {
> reader.close();
> } catch (IOException e1) {
> e1.printStackTrace();
> }
> }
> }
>
> }
>
> }
>
> thanks
> lab


hi

thanks for ur reply. it will also be helpful if u sent the coding
which u have asked me .

waiting for ur reply

regards
D.kavitha

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      02-02-2007
On Wed, 31 Jan 2007 07:55:35 -0500, Lew wrote:
> wrote:
>> while (true) {

>
> To depend on exceptions as flow control is a bad practice.


That may be, but he hasn't done so in this case. The loop condition
here is (curline != null), even though the test doesn't occur until a
few lines further down.

BTW exceptions *are* a form of flow control, whether you like them or
not.

/gordon

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      02-02-2007
Gordon Beaton schrieb:
> On Wed, 31 Jan 2007 07:55:35 -0500, Lew wrote:
>> wrote:
>>> while (true) {

>> To depend on exceptions as flow control is a bad practice.

>

....
>
> BTW exceptions *are* a form of flow control, whether you like them or
> not.


That may be but he didn't tell us if they are or are not a form of
control flow.

To my understanding the key was not to *depend* on exceptions as flow
control or IOW not to (mis)use the exceptional flow to control the main
flow which is right although not always possible.

Bye
Michael
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-02-2007
wrote:
> thanks for ur reply. it will also be helpful if u sent the coding
> which u have asked me .


Juat a word of friendly advice: Your posts will seem much more professional,
and you will be in better practice for workplace communication, if you make a
habit of using more (not completely) formal written English, in particular if
you avoid so-called "l33t-speak" or "txt-English", comprising abbreviations
like "u" for "you", informal or missing punctuation and capitalization, etc.

The trouble is that the use of the abbreviated idiom conveys an impression of
unprofessionalism and a low skill set. This may be the view of yourself that
you wish to convey, but I fail to see how that can help you.

You will impress more, and likely get more expert responses, if you take the
care and trouble to follow conventional English syntax and orthography
(barring the odd misplet word - this forum isn't completely formal).

Just a suggestion designed to help you.

- Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57