1. Member
Join Date
Mar 2012
Posts
3
Rep Power
0

I need to make a function in java that read some stuff from a file .
This is what i need to read :
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 :
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())
{

}

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) {
}```
Any ideas how to to this ?I created an Array list where a want to put all the ecuations .

2. ## 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,

Jos

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 .

4. ## 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,

Jos

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;

while(fis.hasNext())
{
aux=fis.next();

if("end".equals(aux)==true)                  //this is where i read the ecuations
{    i++;
aux=fis.next();}

}
}
//-----------------------------------------------------------------

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;
}
//--------------------------------------------------------```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•