0

How can i read equations from txt file and get these equations coefficients for ex. 3.2x-5.6y=10 is in txt file and i need 3.2 ,-5.6 and 10 for making graph gui program.

I tried bufferedreader but i cant get coefficients.

BufferedReader reader = null;
reader = new BufferedReader(new FileReader("input.txt"));
String line = reader.readLine();
  • What's the format of data in your file? Is it always like what's in your sample? – shree.pat18 Apr 26 '14 at 18:12
  • It's a hw and data must be in an input.txt file.Using split() method i can manage to get x coefficient but i have some issue with y's coefficient – kartal şahin Apr 26 '14 at 18:20
  • That's not what I am asking. Once you make that text file, will all entries in it be in the same format as your sample or not? If yes, then you can look for regular expressions to get the specific parts of the strings that you need. – shree.pat18 Apr 26 '14 at 18:21
  • Thanks,but file can include empty line or not a format of straight line i must check them. – kartal şahin Apr 26 '14 at 18:28

3 Answers3

0

Use Scanner object. You can use it to parse a line with RegExp or any other tools you want.

Emrys Myrooin
  • 2,179
  • 14
  • 39
0

Here is the solution.

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Test {

    /*
     * Constructor just for test
     */
    Test()
    {
        Equation equation = getMembers("-3.2x - 5.6y + 34 = -10");

        System.out.print("Left members: ");
        for(EquationMember m : equation.leftMembers)
            System.out.print(m + " ");

        System.out.print("\nRight members: ");
        for(EquationMember m : equation.rightMembers)
            System.out.print(m + " ");
    }

    /*
     * Main method
     */
    public static void main(String args[])
    {
        new Test();
    }

    /*
     * Transforms a strings representation of an equation into an Equation class
     */
    Equation getMembers(String equationString)
    {
        Equation equation = new Equation();
        String eq = equationString.replace(" ", "");
        char[] charArray = eq.toCharArray();

        boolean isLeft = true;
        String lastMember = "";

        for(int i = 0; i < charArray.length; i++)
        {   
            if(charArray[i] == '-' || charArray[i] == '+' || charArray[i] == '=')
            {
                if(lastMember.length() > 0)
                {
                    EquationMember m = new EquationMember();
                    m.constant = Float.parseFloat(match("([-+]?[\\d\\.]+)[A-Za-z]?", lastMember, 1));
                    m.variable = match("[\\d\\.]+([A-Za-z]?)", lastMember, 1);

                    if(isLeft)
                        equation.leftMembers.add(m);
                    else
                        equation.rightMembers.add(m);
                }
                lastMember = charArray[i] == '=' ? "" : String.valueOf(charArray[i]);
            }
            else
            {
                lastMember += charArray[i];
            }

            if(charArray[i] == '=')
            {
                isLeft = false;
            }
        }

        EquationMember m = new EquationMember();
        m.constant = Float.parseFloat(match("([-+]?[\\d\\.]+)[A-Za-z]?", lastMember, 1));
        m.variable = match("[\\d\\.]+([A-Za-z]?)", lastMember, 1);
        equation.rightMembers.add(m);

        return equation;
    }

    /*
     * Performs a regex match
     */
    String match(String regexp, String text, int group)
    {
        Pattern p1 = Pattern.compile(regexp);
        Matcher m1 = p1.matcher(text);

        if(m1.find())
            return m1.group(group);
        else
            return null;
    }

    /*
     * Class that represents a single member inside an equation
     */
    class EquationMember
    {
        float constant;
        String variable;

        public String toString()
        {
            return constant + "" + variable;
        }
    }

    /*
     * Class that represents an entire equation
     */
    class Equation
    {
        ArrayList<EquationMember> leftMembers;
        ArrayList<EquationMember> rightMembers;

        Equation()
        {
            leftMembers = new ArrayList<EquationMember>();
            rightMembers = new ArrayList<EquationMember>();
        }
    }

}
  • 2
    Wow, concerning the sparse problem description: You must have a glass sphere to come up with that solution. – qqilihq Apr 26 '14 at 19:22
0

You mentioned split, so why not split the string on anything that's not a number?

final String s = "3.2x-5.6y=10";
String[] result = s.split("([^\\d\\.\\+\\-])+");
System.out.println(Arrays.toString(result));
// [3.2, -5.6, 10]

That regex isn't optimal for the general case of numbers (consider 123,456 or invalid input +24-.2), but that can be fixed depending on the requirements.

What this doesn't do is match the variable to the coefficent, but you stated that you only need the values.

lealand
  • 367
  • 4
  • 13