0

下面是一个代码块,它做了两件事 - 函数 readBCInputDataFromTextFile 读取文本文件,并将读取的数据存储在变量 textData 中,该变量是一个字符串数组(即 String[]) 数组大小使用 int 变量填充numOfLines(同样在函数 readBCInputDataFromTextFile 中)。此功能完美运行。

代码所做的第二件事(在函数 checkBCInput 中) - 现在不起作用 - 是使用正则表达式分离数据并将其存储在 4 个“容器”(所有这些都是向量)中:bcStringConstant、bcNameVec、bcTempVec 和bcHTCVec。

现在我的问题是:如何将变量 textData 传递给函数 checkBCInput。如果我将这两个函数的代码合并到一个 java 文件中,代码就可以执行。但是这种让事情发挥作用的方式并不是很好——模块化会受到影响。因此,我需要将代码分成两个函数——一个读取文本文件并将读取的内容传递给第二个函数,第二个函数使用正则表达式将读取的数据存储到特定的容器中。

我尝试从 readBCInputDataFromTextFile 返回 textData,但这在我正在运行的当前模拟中不起作用。我还尝试将读取的数据传递给 checkBCInput——但这会导致另一个错误消息:“找不到符号:符号 textData”。第二种方法(下面以粗体显示)将起作用,但我还看不到解决方案:-/

/*
 * inputReader
 * Last Revision: July 19, 2012.
 */
// package below is specific to software
package macro;

import java.util.*;
import java.text.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// Imports below are specific to the software
import star.base.neo.*;
import star.base.report.*;
import star.common.*;
import star.energy.*;
import star.flow.*;
import star.keturb.*;
import star.material.*;
import star.meshing.*;
import star.metrics.*;
import star.prismmesher.*;
import star.resurfacer.*;
import star.segregatedenergy.*;
import star.segregatedflow.*;
import star.solidmesher.*;
import star.trimmer.*;
import star.turbulence.*;
import star.vis.*;

// base class (StarMacro) is derived from software 
public class inputReader extends StarMacro {
/*************************************************** 
* 
* Global definitions
* 
***************************************************/
// Print output to screen?
int print_to_screen = 0;

// Variables to store BCs             

int noOfBounds = 0;
String inputBCTextFile = null;

Vector bcStringConstant = new Vector();
Vector bcNameVec = new Vector();
Vector bcTempValVec = new Vector();
Vector bcHTCValVec = new Vector();

public void execute(){
    // does nothing but function is required
}

public void readBCInputDataFromTextFile(String inputBCTextFile){

/*  
* READ INPUT FILE CONTENTS
*/

File dir = new File(System.getProperty("user.dir").toString() + File.separatorChar);  
if(print_to_screen == 0){
    sim.println("Directory where input text file is located: " + dir);
}
else{
    System.out.println("Directory where input text file is located: " + dir);
}

this.inputBCTextFile = inputBCTextFile;

if(print_to_screen == 0){
    sim.println("File name is: " + inputBCTextFile);
}
else{
    System.out.println("File name is: " + inputBCTextFile);
}


BufferedReader bf = null;        

try{
    bf = new BufferedReader(new FileReader(inputBCTextFile));

    if(print_to_screen == 0){
        sim.println("FILE EXISTS!!");
    }
    else{
        System.out.println("FILE EXISTS!!");
    }

}

catch (FileNotFoundException e){

    if(print_to_screen == 0){
        sim.println("FILE NOT FOUND!!!");
    }
    else{
        System.err.println("FILE NOT FOUND!!!");
    }

    e.printStackTrace();
}

int numOfLines = 0;
String aLine;

try{
    while (( aLine = bf.readLine()) != null){
        numOfLines++;
    }
} 
catch (IOException e){
    if(print_to_screen == 0){
        sim.println("FILE NOT READ => NUMBER OF LINES IN FILE SET TO NULL");
    }
    else{
        System.err.println("FILE NOT READ => NUMBER OF LINES IN FILE SET TO NULL");
    }
    e.printStackTrace();

}

try{
    bf.close();
}
catch (IOException e){
    if(print_to_screen == 0){
        sim.println("FILE NOT CLOSED PROPERLY!!!");
    }
    else{
        System.err.println("FILE NOT CLOSED PROPERLY!!!");
    }

    e.printStackTrace();
}
BufferedReader textReader = null;

try{
    textReader = new BufferedReader(new FileReader(inputBCTextFile));
    if(print_to_screen == 0){
        sim.println("READING INPUT DATA FROM TEXT FILE");
    }
    else{
        System.out.println("READING INPUT DATA FROM TEXT FILE");
    }

} 
catch (FileNotFoundException e){
    if(print_to_screen == 0){
        sim.println("FILE NOT FOUND!!!");
    }
    else{
        System.err.println("FILE NOT FOUND!!!");
    }

    e.printStackTrace();
}

String[] textData = new String[numOfLines];

try{
for (int i = 0; i < numOfLines; i++){
    textData[i] = textReader.readLine();
    }
}
catch (IOException e){
    if(print_to_screen == 0){
        sim.println("FILE NOT READ!!!");
    }
    else{
        System.err.println("FILE NOT READ!!");                
    }

    e.printStackTrace();
}

 **checkBCInput(textData);**

try{
    if(print_to_screen == 0){
        sim.println("FILE READ AND CLOSED");
    }
    else{
        System.out.println("FILE READ AND CLOSED");
    }

    textReader.close();
    }
catch (IOException e){
    if(print_to_screen == 0){
        sim.println("FILE NOT CLOSED PROPERLY!!!");
    }
    else{
        System.err.println("FILE NOT CLOSED PROPERLY!!!");
    }

    e.printStackTrace();
   }

 }

public void checkBCInput(String[] textData){
    Pattern inputBCTextFileData = Pattern.compile("(.*)\t(.*)\t-?((\\d+\\.\\d*|\\d*\\.\\d+)|\\d+)\t-?((\\d+\\.\\d*|\\d*\\.\\d+)|\\d+)");

for (int i =0; i < textData.length; i++){
    Matcher inputBCTextFileDataMatcher = inputBCTextFileData.matcher(textData[i]);

    if(inputBCTextFileDataMatcher.find()){
        // 1st match is variable name
            bcStringConstant.add(inputBCTextFileDataMatcher.group(1)); // this is the 1st column in the text file
        bcNameVec.add(inputBCTextFileDataMatcher.group(2));
        bcTempValVec.add(inputBCTextFileDataMatcher.group(4)); // gets numbers in decimal notation
        bcHTCValVec.add(inputBCTextFileDataMatcher.group(6)); // gets numbers in decimal notation
    }
 }

 }  

}

4

1 回答 1

0

我尝试从 readBCInputDataFromTextFile 返回 textData,但这在我正在运行的当前模拟中不起作用。

为什么不?像这样的东西应该工作:

String[] textData = readBCInputDataFromTextFile(...);
checkBCInput(textData);

然后更改 to 的返回类型readBCInputDataFromTextFile并在其末尾String[]添加 a 。return textData;

于 2012-07-24T17:52:36.560 回答