my dog learned polymorphism*
The moose likes Java in General and the fly likes to read lines inside brackets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "to read lines inside brackets" Watch "to read lines inside brackets" New topic
Author

to read lines inside brackets

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
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?


Ping & DNS - my free Android networking tools app
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Mar 06, 2001
Posts: 13459

You can read each line and match the String against a regular expression
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I cant understand.could you show me in codes?

Thanks
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
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

Joined: Oct 04, 2007
Posts: 518
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
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
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

Joined: Oct 04, 2007
Posts: 518
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
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
I was thinking along the lines of
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
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

Joined: Oct 04, 2007
Posts: 518
ok fine... can you show me some sample coding examples...so that i may get an idea....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
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

Joined: Oct 04, 2007
Posts: 518
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
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
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

Joined: Oct 04, 2007
Posts: 518
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
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
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

Joined: Aug 05, 2005
Posts: 3498
    
  13
Also, you are printing out the value of the 'strLine' variable. You should be printing the value of the 'value' variable.


Joanne
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
HI,
I have altered the changes as you said.But i am getting null pointer exception.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
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

Joined: Oct 04, 2007
Posts: 518
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

Joined: Aug 05, 2005
Posts: 3498
    
  13
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

Joined: Oct 04, 2007
Posts: 518
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
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
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

Joined: Oct 04, 2007
Posts: 518
hi......sorry for delayed reply....atlast its workin......thousands of thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: to read lines inside brackets