[英]Having read a text file in java, how do i pass the read content from the function that reads the text file to some other function in the same class?
Below is a block of code that does two things - the function readBCInputDataFromTextFile reads in a text file, and stores the read data in the variable textData which is a string array (ie, String[]) The array size is populated using the int variable numOfLines (again in function readBCInputDataFromTextFile). 下面的代码块可以完成两件事-函数readBCInputDataFromTextFile读取文本文件,并将读取的数据存储在作为字符串数组的变量textData中(即String [])。使用int变量填充数组大小numOfLines(同样在函数readBCInputDataFromTextFile中)。 This function works perfectly.
此功能运行完美。
the second thing that the code does (in function checkBCInput) - and which doesn't work right now - is separate data using regular expresssions and store it in 4 "containers" (all of which are vectors): bcStringConstant, bcNameVec, bcTempVec & bcHTCVec. 代码执行的第二件事(在checkBCInput函数中)(现在不起作用)是使用正则表达式分离数据并将其存储在4个“容器”(所有容器都是向量)中:bcStringConstant,bcNameVec,bcTempVec和bcHTCVec。
Now my question is: how do I pass the variable textData to function checkBCInput. 现在我的问题是:如何将变量textData传递给函数checkBCInput。 If I combine code from both functions into one single java file, the code works on execution.
如果我将两个函数的代码合并到一个单独的java文件中,则代码将在执行时起作用。 But this way of getting things to work isn't very good - modularising suffers.
但是,这种使事情正常进行的方法不是很好-模块化会遭受损失。 Hence the need on my part to separate the code into two functions - one that reads a text file and passes the read contents to the second function, which using regex stores the read data into specific containers.
因此,就我而言,需要将代码分为两个功能-一个功能是读取文本文件并将读取的内容传递给第二个功能,第二个功能使用regex将读取的数据存储到特定的容器中。
I tried returning textData from readBCInputDataFromTextFile, but that won't work in the current simulation that I'm running. 我尝试从readBCInputDataFromTextFile返回textData,但是在我正在运行的当前模拟中无法使用。 I also tried to pass the read data to checkBCInput - but that lead to another error message: "Cannot find symbol: Symbol textData".
我还尝试将读取的数据传递给checkBCInput-但这导致另一个错误消息:“找不到符号:符号textData”。 The second method (shown in bold below) is going to work, but I can't see the solution yet :-/
第二种方法(下面以粗体显示)将起作用,但是我仍然看不到解决方案:-/
/*
* 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
}
}
}
} }
I tried returning textData from readBCInputDataFromTextFile, but that won't work in the current simulation that I'm running.
我尝试从readBCInputDataFromTextFile返回textData,但是在我正在运行的当前模拟中无法使用。
Why not? 为什么不? Something like this should work:
这样的事情应该起作用:
String[] textData = readBCInputDataFromTextFile(...);
checkBCInput(textData);
Then change the return type of readBCInputDataFromTextFile
to String[]
and add a return textData;
然后将
readBCInputDataFromTextFile
的返回类型readBCInputDataFromTextFile
为String[]
并添加一个return textData;
at the end of it. 在它的结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.