【问题标题】:java set of code does not executejava代码集不执行
【发布时间】:2013-06-06 13:05:05
【问题描述】:

我有一个 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]); 

}                

【问题讨论】:

  • 如果您正确缩进代码,阅读代码会更容易。您还应该遵循正常的 Java 命名约定。最后,您发布了很多代码——我怀疑您实际上不需要向我们展示所有代码来演示问题。您还没有真正描述问题 - 您已经说过“没有其他方法有效”,但不是您期望它做的与实际做的。
  • 抱歉缩进。我认为如果可以将代码作为一个整体给出,那么识别问题会更容易。问题是我想将数组从 main 传递到 ParsedAnalysis 类,在那里我想在某些条件下获取特定的数组元素。 myArray 中的每个元素都是一个字符串。在我的代码中,我要显示 SUB[l] 的输出。即使它没有得到价值,它也应该打印出一些东西。但是什么都没有显示。我觉得代码根本没有被执行。

标签: java arrays parameter-passing response


【解决方案1】:
  1. 以下条件无效:

    if(myArray[i].contains("(") &amp;&amp; myArray[i].contains(")") )

    数组中的同一个地方不能同时保存“(”和“)”——这个条件总是返回false!

  2. 数组 SUB 从未初始化。

备注:
for(i=0;!(line=reader.readLine()).isEmpty() &amp;&amp; i&lt;20;i++) 会停在文件的第一个空行,但有时有空行你还想继续阅读,如果是这种情况你应该如果! isEmpty(),则使用!= null

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2013-04-21
    • 1970-01-01
    • 2012-07-08
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多