我有一个java代码,其中parse方法返回一个数组到main方法。该数组被传递给 Parsedanalysis 类的 OutputFilter 方法。但是OutputFilter方法无法使用这个数组。当我尝试打印 myArray[] 时,它起作用了。但其他都不起作用输出仅显示构建成功。我不明白。能否请您提供解决方案。
提前致谢。
public class Parser {
public static void main(String args[]) throws IOException, InterruptedException
{
String[] parseOutput;
parseOutput = parse();
Parsedanalysis p =new Parsedanalysis();
p.OutputFilter(parseOutput);
System.out.println("Output returned array"); //Output Verification//
for(int i=0;i<parseOutput.length;i++)
{ System.out.println(parseOutput[i]);
}
}
public synchronized static String[] parse() throws IOException, InterruptedException
{
{
String[] output = new String[20];
int i=0;
String command = "cmd /k cd C:\\Program Files\\stanford-parser-2012-11-12 & "
+ "set CLASSPATH=.;stanford-parser.jar;stanford-parser-2.0.4-models.jar & "
+ "java -mx100m edu.stanford.nlp.parser.lexparser.LexicalizedParser edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz data/testsent.txt"; //without typed dependencies
Process pr = Runtime.getRuntime().exec(command);
BufferedReader reader=new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = reader.readLine();
System.out.println("Output array");
for(i=0;!(line=reader.readLine()).isEmpty() && i<20;i++)
{
output[i]=line;
System.out.println("Position"+i+output[i]);
}
// System.out.println("stopped");
reader.close();
return(output);
}
public class Parsedanalysis {
static int start = 0;
static int end;
static String[] POS,SUB,array;
/**
*
* @param myArray
* @throws IOException
* @throws InterruptedException
*/
public synchronized void OutputFilter(String[] myArray) throws IOException, InterruptedException
{
int l=0;
try{
System.out.println("Parsedanalysis Recieves..");
for(int k=0;k<myArray.length;k++)
{
//System.arraycopy(myArray, 0, array, 0, myArray.length); /*doesn't work beyond this point*/
System.out.println(myArray[k]);
//array[k] = myArray[k];
}
for(int i=0;i<myArray.length;i++)
{
if(myArray[i].contains("(") && myArray[i].contains(")") )
{
System.arraycopy(myArray, i, SUB, l, 1);
//SUB[l]=myArray[i];
l++;
}
System.out.println(SUB[l]);
}
最佳答案
以下条件无效:
if(myArray[i].contains("(") && myArray[i].contains(")") )
数组中的同一位置不能同时容纳“(”和“)” - 此条件将始终返回 false!
数组 SUB 从未初始化。
备注:
for(i=0;!(line=reader.readLine()).isEmpty() && i<20;i++)
将停止在文件中的第一个空行,但有时有空行,您仍然想继续阅读,如果是这种情况,您应该使用 != null
相反,如果 ! isEmpty()
关于java一组代码不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17031274/