Results 1 to 5 of 5
Thread: Read ecuations from file
- 03-30-2012, 04:25 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Read ecuations from file
I need to make a function in java that read some stuff from a file .
This is what i need to read :
It's some values and some ecuations .n= 3
( 432 + x^4 + y^4 - 2z^4 + y^2 * z^2 ) / 234
( 324 - x^4 + 2y^4 - 3z^4 + 3x^2 * z^2 ) / 432
( 234 + 2x^4 - 3y^4 + 4z^4 - x^2 * y^2 + 3x + y ) / 324
x0= 0 0 0,5
q= 0,33
E= 10^-10
This is what i coded so far :
Any ideas how to to this ?I created an Array list where a want to put all the ecuations .Java Code:public void ReadFile() throws FileNotFoundException { try{ Scanner fis = new Scanner( new FileInputStream("input.txt")); if("n=".equals(fis.next("n="))==true ) { n=Integer.parseInt(fis.next()); System.out.println("n=" + n); } while(fis.hasNext()) { matrix.add(new ArrayList()); ((ArrayList)matrix.get(0)).add(fis.next()); } for(i=0;i< matrix.size();i++){ for(j=0;j< ((ArrayList)matrix.get(i)).size();j++) { System.out.print( ((ArrayList)matrix.get(0)).get(j) + " "); }// System.out.println(); } } catch( IOException ioException) { throw new RuntimeException("File not found !");} }
- 03-30-2012, 04:32 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
Re: Read ecuations from file
Do you need those formulas as just Strings or do you need them as if they are real expressions?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-30-2012, 04:42 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Re: Read ecuations from file
I need them to do some mathematical stuff ,so real expressions .
- 03-30-2012, 05:17 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
Re: Read ecuations from file
Well, you're in quite some work then: you either have to write an almost complete formula/expression parser (also see my blog entry on expression compilers) or you have to include a script interpreter such as the included Javascript engine. Both have their pros and cons. There is no free lunch here.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-02-2012, 12:03 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Re: Read ecuations from file
I reached a impas with my project. I read the expressions in a ArrayList called "matrix" and for mathematical operations i copy the Arraylist into another ArrayList called "clone" . Why when the statements at lines 33, 34,35 are executed affects both ArrayLists ?
I didn't posted all my code because is ~550 lines
Java Code:ArrayList matrix= new ArrayList () ; ArrayList clone =new ArrayList(); .... //----------------------------------------------- i=0; matrix.add(new ArrayList()); clone.add(new ArrayList()); while(fis.hasNext()) { aux=fis.next(); if("end".equals(aux)==true) //this is where i read the ecuations { i++; clone.add(new ArrayList()); matrix.add(new ArrayList()); aux=fis.next();} ((ArrayList)matrix.get(i)).add(aux); } } //----------------------------------------------------------------- public Double calcExp(int poz) { suma=0; Collections.copy(clone,matrix); // i need to copy matrix into clone every time this function is called for(j=0;j< ((ArrayList)clone.get(poz)).size();j++) { if("^".equals( ((ArrayList)clone.get(poz)).get(j)) && ")".equals( ((ArrayList)clone.get(poz)).get(j-1))==false) { ((ArrayList)clone.get(poz)).set(j,calcExpPow(poz,j)); //calcExpPow returns a double ((ArrayList)clone.get(poz)).remove(j+1); ((ArrayList)clone.get(poz)).remove(j-1); j--; System.out.println("pow"); this.Display(clone); } System.out.println(" ^"); this.Display(matrix);//this shouldn't change but displays same elements like clone } ... return suma; } //--------------------------------------------------------
Similar Threads
-
Read CSV File, sort records, and output a new csv file
By jrnowlan in forum New To JavaReplies: 1Last Post: 08-05-2011, 09:21 PM -
Read file from directory, update contents of the each file
By svpriyan in forum New To JavaReplies: 2Last Post: 05-11-2009, 10:07 AM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks