我需要拆分和字符串拆分它使用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