Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

to read lines inside brackets

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In my program ,i am reading a text file which consists of 100 lines.I need only to display the lines inside the brackets.how can i do this?please show me sample codes .
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would need to examine the lines you're reading, to see if they contain an opening bracket. Once found, you would start to display those lines. You'd continue to examine the lines for a closing bracket, of course. Can there be brackets that shouldn't start or end the display, like inside of a string literal or a comment? What do you have so far?
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I put some lines of the text file below .please tell me how to read that particular line.

[general]
Global settings for call queues
Persistent Members
Store each dynamic agent in each queue in the astdb so that
when asterisk is restarted, each agent will be automatically

member => Agent/0
member => Agent/0
member => Agent/1001
member => Agent/1002
:setting ebug:true
:setting:log:true

[peoplesys]
eventwhencalled = yes
strategy = leastrecent
timeout = 10
wrapuptime = 1.
Now i need to read lines from the member and lines inside the[peoplesys].

how shall i read those particular lines and display?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read each line and match the String against a regular expression
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant understand.could you show me in codes?

Thanks
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my code is given below which prints all the lines in the text file.
Code:

import java.io.*;
class FileRead1
{
public static void main(String args[])
{
try{

FileInputStream fstream = new FileInputStream("myfile.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine())!=null)
{
System.out.println (strLine);

}
in.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
}
what i have to alter in this code to read and print the particular lines
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're looking for the lines in square brackets, right? How can you find out if a string starts with an opening square bracket, and ends with a closing square bracket? You'll probably want to remove leading and trailing white space first; this can be done with the String.trim method.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will tell you what i need to do exactly.here is my text file below.

use agent groups.
available, but consider with penalty
member => Agent/0
member => Agent/0
member => Agent/1001
member => Agent/1002
setting ebug:true
setting:log:true
[peoplesys]
eventwhencalled = yes
strategy = leastrecent
timeout = 10
wrapuptime = 1
setting:active:true
setting:function redictive
setting:maxratio:2
setting:maxabandons:3.0.

From these lines first of all i have to read the line setting:active:true and check if it is true.if yes, i have to read the line setting:maxratio:2 and then i have to read the member lines .how can i do this?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already have code that reads a file line by line, and hands you each line as a String object. Are you familiar with the methods of String? For example, the startsWith method would be helpful with this, as would lastIndexOf.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ulf,I think startsWith method wont help me.actually i had already done a program in jsp to read a text file and to read the particular columns to insert into particular fields like this.
text file:
HDR1234uyt56788kiolhj67777
from the above line i have to separate the fields.In that case, i used the below method to separate the fields:

String a=line.substring(0,3);

String b=line.substring(3,10);

String c=line.substring(10,15);
Here(0,3) mentions the column count.Like this could i read the lines using the row count?Is there any way?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking along the lines of
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its some wat similar but if the line ,setting active should be equal to true only... i should read max lines and maxratio...otherwise i should'nt read...but your sample code doesn'nt seem to be like that
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How have you tried to use it? You will need to add a few things, like a boolean that keeps track of the value of setting:active and a couple of strings or ints for the values of the two other lines you want to read.

You seem to want to read the max[ratio] lines only if the setting is true; that is not readily possible, because files are sequential - you have to read all lines. Of course, once you have determined that the setting is false, you can just stop reading from the file altogether.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok fine... can you show me some sample coding examples...so that i may get an idea....
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give an example of what? There's maybe 6 lines of code to add to what I posted earlier. I'm a bit hard-pressed how to describe the idea in more detail, because there's really not much to it. How have you tried to integrate that code with yours?
[ December 08, 2007: Message edited by: Ulf Dittmer ]
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
could i read the nth line with the help of line number reader ?if the line number of the text file is stable at any time?

Thanks
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you want to make it more complicated than necessary, and introduce a dependence on the position in the file (which would break as soon as someone puts an extra comment line into it).
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried as you said.but this just displays the strLine.thats why i thought to try with line number reader.apologise if iam going badly wrong.
import java.io.*;
class FileRead1
{
public static void main(String args[])
{
try{

FileInputStream fstream = new FileInputStream("queues.conf");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine=":setting:active" ;
String strLine1=null;

if(strLine.startsWith(":setting:active")) {
String value = strLine.substring(strLine.lastIndexOf(":")+1);
System.out.println(strLine);
}
else if (strLine1.startsWith("setting:maxratio")){
String value = strLine1.substring(strLine1.lastIndexOf(":")+1);
System.out.println(strLine1);


in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted doesn't compile; please post working code. Also be sure to UseCodeTags so that the code is formatted.

The code doesn't read from the file - what happened to the while loop and the readLine statement? You also seem to use strLine and strLine1 in a way that looks incorrect to me.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you are printing out the value of the 'strLine' variable. You should be printing the value of the 'value' variable.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
I have altered the changes as you said.But i am getting null pointer exception.

 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while((strLine=br.readLine())!=null)
{
strLine=br.readLine();

You're reading twice from the file in each loop iteration - remove the "strLine=br.readLine();" line.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.Now i am getting the output printed as true.then i want to check whether the value==true,then the line :setting:maxratio: will be checked for the ratio.for that i tried the following.
.
I am getting the error
FRead.java:22: cannot find symbol
symbol : variable value
location: class FRead
if(value=="true")
^
[ December 11, 2007: Message edited by: preethi Ayyappan ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. value is declared inside the if block, therefore it is out of scope at line 22.

2. Don't use == to compare Strings, use String.equals method.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.Now i made changes like this.

.
It is showing the error:
java.lang.NullPointerException
at FRead.main(FRead.java:23)
what should i initialize in value?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you're getting a NullPointerException is that you still have the "String value = ..." in the if-statement. You should remove the "String", so that it assigns the value to the variable declared outside of the while loop.

Since you put a fair amount of effort into this, I'll deviate from the usual rule and post some complete code. It reads the file completely before doing anything with the values it has read. That makes for a cleaner code structure by separating the file reading from the value handling.

[ December 11, 2007: Message edited by: Ulf Dittmer ]
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi......sorry for delayed reply....atlast its workin......thousands of thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic