aspose file tools*
The moose likes Beginning Java and the fly likes how to split in java and remove certain lines from that text? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to split in java and remove certain lines from that text?" Watch "how to split in java and remove certain lines from that text?" New topic
Author

how to split in java and remove certain lines from that text?

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
hi all,

Good evening.. i am having an file in that file i want to split the ":" colon . the words are split after and before colon . after the colon split i have i have an example given like this

input

Rule1.1.1 : this is rule one
EG. Ramakalmedu Located at a distance of 16 km from Nedumkandam
Rule1.1.2 : this is rule two
EG. word starting with a in java
Rule1.1.4 : this is rule three
EG. word starting with a in java
Rule1.3.1 : this is rule four
EG. Summer is the season to visit Mumbai
Rule1.3.9 : this is rule five
EG. It is operational only from march to september




here rule_no represents Rule1.1.1, Rule1.1.2 etc.. and rule represents the sentences
i am getting an error in this rule.

can anny one tell how to get the rule number and rule alone, leaving the EG.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

deepika deepi wrote:i am getting an error in this rule.

What error?

However, off the top of my head: if your input is exactly as you showed it (ie, it includes the lines starting "EG."), then it doesn't conform to your own specification; particularly if you are passing each line to those 4 lines of code.

BTW, don't use names like "$_"; they're OK for perl or Python, but you'll just confuse Java-ites.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jo Joseph
Greenhorn

Joined: Nov 28, 2010
Posts: 23
I think This will help you...

class Test{
public static String str ="Rule1.1.1 : this is rule one EG. Ramakalmedu Located at a distance of 16 km from Nedumkandam";
public static void main(String[] args) {
String[] splitRuleNo = str.split("\\s*\\:\\s*");
String rule_no = splitRuleNo [0]; //\\ Mr
String rule = splitRuleNo [1];//+" "+splitResult[2]+" "+splitResult[3]; //\\ John Smith Dickson
String[] splitRule = rule.split("\\s*\\.\\s*");
System.out.println(rule_no);
System.out.println(splitRule[1]);
}
}
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
hi yaar,

that code is fine. but what i am telling is slightly different. you have taken the sentence in single string. but what i have given is it comes in two string. The input is like this

Rule1.1.1 : this is rule one
EG. Ramakalmedu Located at a distance of 16 km from Nedumkandam


and not like this

Rule1.1.1 : this is rule one EG. Ramakalmedu Located at a distance of 16 km from Nedumkandam. that is the problem i am facing here. Hope i am clear.
Jo Joseph
Greenhorn

Joined: Nov 28, 2010
Posts: 23
If the input is coming from the file or console you can very well use the "BufferedReader" and assign to a string, then can do the exact manipulation

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

public static void main(String[] args) {

BufferedReader br = null;

try {

String sCurrentLine;

br = new BufferedReader(new FileReader("C:\\testing.txt"));

while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);

String[] splitRuleNo = sCurrentLine.split("\\s*\\:\\s*");
String rule_no = splitRuleNo [0]; //\\ Mr
String rule = splitRuleNo [1];//+" "+splitResult[2]+" "+splitResult[3]; //\\ John Smith Dickson
String[] splitRule = rule.split("\\s*\\.\\s*");
System.out.println(rule_no);
System.out.println(splitRule[1]);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

}
}
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199


i am getting an array exception in the code as follows:


Rule1.1.1 : this is rule one
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at BufferedReaderExample.main(BufferedReaderExample.java:25)

(i.e) in this line

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
The reason for that exception is obvious: you do not have two members in that array. Maybe you haven’t split the input properly. Have a look at the section about how to use String.split() in this tutorial page.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to split in java and remove certain lines from that text?