473,325 Members | 2,860 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,325 software developers and data experts.

How to read from a .txt and match the strings.

I Have a file name.txt.
Below is the format of names.txt.

1 SATYA;
2 KUMAR;
3 MOHAN;
4 Venkat;

In my java code i get an integer(say 3).
With that integer(3) i should pick the corresponding name from the names.txt and display on the screen.
How do i do that?

if input is 3 the display must be MOHAN.
if input is 4 the display must be Venkat.
if input is 2 the display must be KUMAR.


Please send few lines of code for this operation.
Jan 17 '07 #1
5 1658
r035198x
13,262 8TB
I Have a file name.txt.
Below is the format of names.txt.

1 SATYA;
2 KUMAR;
3 MOHAN;
4 Venkat;

In my java code i get an integer(say 3).
With that integer(3) i should pick the corresponding name from the names.txt and display on the screen.
How do i do that?

if input is 3 the display must be MOHAN.
if input is 4 the display must be Venkat.
if input is 2 the display must be KUMAR.


Please send few lines of code for this operation.
You write the code first and post it. Use
Expand|Select|Wrap|Line Numbers
  1. BufferedReader br = new BufferedReader(new FileReader("fileName.txt")); 
to open the file
and
Expand|Select|Wrap|Line Numbers
  1. br.readLine(); 
to read a line from the file
Jan 17 '07 #2
You write the code first and post it. Use
Expand|Select|Wrap|Line Numbers
  1. BufferedReader br = new BufferedReader(new FileReader("fileName.txt")); 
to open the file
and
Expand|Select|Wrap|Line Numbers
  1. br.readLine(); 
to read a line from the file

I want without BufferedReader. i.e, with InputStreamReader.

Can you suggest me a way to through this.
Jan 17 '07 #3
r035198x
13,262 8TB
I want without BufferedReader. i.e, with InputStreamReader.

Can you suggest me a way to through this.
That is very bad practice. You should use a Scanner, BufferedReader or RandomAccessFile for this.

Why do you want to use InputStreamReader?
Jan 17 '07 #4
That is very bad practice. You should use a Scanner, BufferedReader or RandomAccessFile for this.

Why do you want to use InputStreamReader?
We have restricted pacakge access.We use our own packages derived from java api. Hence we cant use all the classes in the Java API.
Jan 17 '07 #5
r035198x
13,262 8TB
We have restricted pacakge access.We use our own packages derived from java api. Hence we cant use all the classes in the Java API.
I don't understand this. You have access to InputStreamReader but not to RandomAccessFile or FileReader? What other io classes are you not allowed to use then because the InputStreamReader alone cannot read a file.


Here is an exctract of the API specs


An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specifiedcharset .The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example: BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Jan 17 '07 #6

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

Similar topics

2
by: Sriram | last post by:
Hi, I am having trouble matching a regex that combines a negated character class and an anchor ($). Basically, I want to match all strings that don't end in a digit. So I tried: ...
43
by: Roger L. Cauvin | last post by:
Say I have some string that begins with an arbitrary sequence of characters and then alternates repeating the letters 'a' and 'b' any number of times, e.g. "xyz123aaabbaabbbbababbbbaaabb" I'm...
54
by: smnoff | last post by:
Below is a section from string.c at this linkhttp://cvs.opensolaris.org/source/xref/on/usr/src/common/util/string.cthat I am trying to fully understand.I don't fully understand LINE 514; not to...
9
by: a | last post by:
I need to write a regular expression to match a quoted string in which the double quote character itself is represented by 2 double quotes. For example: "beginning ""nested quoted string"" end"...
32
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression...
4
by: Peter | last post by:
Hi all, I am searching through directories trying to find the prefix to a number of files. Unfortunately the files don't have a standard naming convention yet. So some of them appear as:...
2
by: dayanand ramasubbu | last post by:
Hi, I want to get the best possible match for a strings among a list of strings. For eg, my string to be matched is hi/howareyou?rudoingok which needs to be matched against the below list, ...
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
4
by: Dylan Nicholson | last post by:
I can write a regular expression that will only match strings that are NOT the word apple: ^(.*|a.*|ap.*|app.*|apple.+)$ But is there a neater way, and how would I do it to match strings that...
6
by: =?iso-8859-1?q?C=E9dric_Lucantis?= | last post by:
Le Thursday 26 June 2008 15:53:06 oyster, vous avez écrit : The construct does not match a whole word but only one char, so means "any char which is not t, a, b, l or e". Anyway the inside...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.