• 03-30-2012, 04:25 PM
drunkkiller
I need to make a function in java that read some stuff from a file .
This is what i need to read :
Quote:

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
It's some values and some ecuations .

This is what i coded so far :
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 !");}   }```
Any ideas how to to this ?I created an Array list where a want to put all the ecuations .
• 03-30-2012, 04:32 PM
JosAH
Do you need those formulas as just Strings or do you need them as if they are real expressions?

kind regards,

Jos
• 03-30-2012, 04:42 PM
drunkkiller
I need them to do some mathematical stuff ,so real expressions .
• 03-30-2012, 05:17 PM
JosAH
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,

Jos
• 04-02-2012, 12:03 AM
drunkkiller
```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; } //--------------------------------------------------------```