我需要拆分和字符串拆分它使用Java
Sentence:NounPhrase| VerbPhrase
NounPhrase:Art| Noun
Sample:the
这应该写成
Sentence:NounPhrase
Sentence :VerbPhrase
NounPhrase:Art
NounPhrase: Noun
Sample:the
我怎么可以这样使用Java
编辑
文件expression.txt
Sentence:NounPhrase VerbPhrase
NounPhrase:Art Noun
VerbPhrase:Verb|Adverb Verb
Art:the|a
Verb:jumps|sings
Noun:dog|cat
节目我用,但不工作
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
public class shift {
public static String readFileFull(String file)
{
String strLine = null;
StringBuffer sb = new StringBuffer();
try{
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((strLine = br.readLine()) != null) {
sb.append("\n");
sb.append(strLine);
}
in.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
String ret=sb.toString();
return ret;
}
public static void main(String args[]) {
String speech = readFileFull("c://expression.txt");
StringBuilder sb = new StringBuilder();
Scanner sc = new Scanner(speech);
while (sc.hasNextLine()) {
String[] ps = sc.nextLine().split(":");
for (String s : (ps[1] + "|").split("\\|"))
if (!s.equals(""))
sb.append(ps[0]+":").append(s).append("\n");
}
System.out.println(sb.toString());
}
}
,并试图告诉我易我仍然得到错误
不适用于多行 – sundhar 2010-11-01 17:48:22
@ Sundhar..Well ...您需要将您的输入逐行输入到上面的代码片段才能使其正常工作......您如何获得输入?你从文件中读取?或者它是一个单一的字符串? – 2010-11-01 17:51:58
我正在阅读文件 – sundhar 2010-11-01 17:52:37