I am having this issue getting the file input from one class and using it in another. So what happens is I have a file called readFile.java that reads in the line of a txt file. I have another file that I am using to evaluate a stack that I want to use the file input. So all in all, I am trying to find a way to replace my testInput string in my evalStack.java file with the file input from the readFile.java.
Here is the readFile.java:
import java.util.Scanner;
import java.io.*;
public class readFile {
String fname;
public readFile() {
System.out.println("Constructor");
getFileName();
readFileContents();
}
public void readFileContents()
{
boolean looping;
DataInputStream in;
String line;
int j, len;
char ch;
/* Read input from file and process. */
try {
in = new DataInputStream(new FileInputStream(fname));
looping = true;
while(looping) {
/* Get a line of input from the file. */
if (null == (line = in.readLine())) {
looping = false;
/* Close and free up system resource. */
in.close();
}
else {
System.out.println("line = "+line);
j = 0;
len = line.length();
for(j=0;j<len;j++){
System.out.println("line["+j+"] = "+line.charAt(j));
}
}
} /* End while. */
} /* End try. */
catch(IOException e) {
System.out.println("Error " + e);
} /* End catch. */
}
public void getFileName()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter file name please.");
fname = in.nextLine();
System.out.println("You entered "+fname);
}
}
This is the evalStack.java:
import java.util.Scanner;
import java.io.*;
public class evalStack {
static String testInput = "(6+3) + (3-2)";
//This is the line I want to replace with the input the readFile gives me.
public static void main(String[] args){
int maxLength = testInput.length();
stackOb eval = new stackOb(maxLength);
boolean test = false;
//Evaluate and check parenthesis
for(int i = 0; i < testInput.length(); i++)
{
char a = testInput.charAt(i);
if(a=='(')
{
eval.push(a);
}
else if(a==')')
{
if(eval.empty() == false)
{
eval.pop();
}
else
{
test = true;
System.out.println("The equation is a not valid one.");
System.exit(0);
}
}
else
{
continue;
}
}
if(eval.empty() == true && test == false)
{
System.out.println("The equation is a valid one.");
}
else
{
System.out.println("The equation is a not valid one.");
}
}