Using txt file input in a different class in java -
i having issue getting file input 1 class , using in another. happens have file called readfile.java reads in line of txt file. have file using evaluate stack want use file input. in all, trying find way replace testinput string in evalstack.java file file input readfile.java.
here 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 file , process. */ try { in = new datainputstream(new fileinputstream(fname)); looping = true; while(looping) { /* line of input file. */ if (null == (line = in.readline())) { looping = false; /* close , free 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 evalstack.java:
import java.util.scanner; import java.io.*; public class evalstack { static string testinput = "(6+3) + (3-2)"; //this line want replace input readfile gives me. public static void main(string[] args){ int maxlength = testinput.length(); stackob eval = new stackob(maxlength); boolean test = false; //evaluate , check parenthesis for(int = 0; < testinput.length(); i++) { char = 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 not valid one."); system.exit(0); } } else { continue; } } if(eval.empty() == true && test == false) { system.out.println("the equation valid one."); } else { system.out.println("the equation not valid one."); } }
- add line
import readfile;
top of evalstack.java - change input:
static string testinput = new readfile().readfilecontents();
- change return type:
public string readfilecontents()
- replace
j = 0;
return line;
that make evaluate first line.
Comments
Post a Comment