Results 1 to 5 of 5
  1. #1
    drunkkiller is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default 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 :
    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())   
    {  
    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 .

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,247
    Blog Entries
    7
    Rep Power
    24

    Default 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
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    drunkkiller is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Read ecuations from file

    I need them to do some mathematical stuff ,so real expressions .

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,247
    Blog Entries
    7
    Rep Power
    24

    Default 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
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    drunkkiller is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default 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

  1. Replies: 1
    Last Post: 08-05-2011, 09:21 PM
  2. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  3. Replies: 0
    Last Post: 02-11-2009, 10:53 AM
  4. Replies: 5
    Last Post: 02-05-2009, 11:28 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM

Posting Permissions

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