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 (i.e, String[]) The array size is populated using the int variable numOfLines (again in function 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.
Now my question is: how do I pass the variable textData to function checkBCInput. If I combine code from both functions into one single java file, the code works on execution. 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.
I tried returning textData from readBCInputDataFromTextFile, but that won’t work in the current simulation that I’m running. I also tried to pass the read data to checkBCInput – but that lead to another error message: “Cannot find symbol: Symbol 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
}
}
}
}
Why not? Something like this should work:
Then change the return type of
readBCInputDataFromTextFiletoString[]and add areturn textData;at the end of it.