2

The goal is to create a program that takes a quadratic equation in quadratic form and solve it. Is there a different way to go about doing so other than StringTokenizer? Or is it possible to isolate just ^2 in StringTokenizer rather than ^ and 2 like it is doing now? I realized that using the way I wrote it, it will not allow equations to use 2 at all.

This question requires me to not take individual coefficients, but rather the entire equation itself.

Sample run: ”java SolveEquation2 1.5625x∧2+2.5x+1=0”. For this input the output should be: ”x=-0.8”

import java.util.Scanner;
import java.util.StringTokenizer;
class SolveEquation2 {
    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
        System.out.print("Input a quadratic");
        String equation = scan.nextLine();

        StringTokenizer st = new StringTokenizer(equation, "x^2+-");



        String a,b,c;

        a = st.nextToken();
        b = st.nextToken();
        c = st.nextToken();

        double a1 = Double.parseDouble(a);
        double b1 = Double.parseDouble(b);
        double c1 = Double.parseDouble(c);


        double x = (b1 * b1) - (4 * a1 * c1);

        double var1 = (-b1 + Math.sqrt(x)) / (2*a1);
        double var2 = (-b1 - Math.sqrt(x)) / (2*a1);

        if (x == 0){
            System.out.println("x = " + var1);

        }
        if (x > 0){
            System.out.println("x1 = " + var1);
            System.out.println("x2 = " + var2);
        }
        if (x < 0){
            System.out.println("No Solution");


    }
    }
}
metacubed
  • 7,031
  • 6
  • 36
  • 65
  • 1
    Do you have to use this input format? It would be easier IMO if you collect your coefficients in individual scans – ControlAltDel Feb 11 '15 at 16:10
  • You can have the entire equation as a string and then split the string using any +/- sign. See String.split(regex)` in javadoc. Also as @ControlAltDel suggests, separate scans (i.e. nextDouble, nextFloat, etc.) unless you have a requirement to do it this way. – ha9u63a7 Feb 11 '15 at 16:12
  • 1
    Not directly related to your question, but what if the discriminant is negative ? and what if a1 is 0 ? Consider handling those as well. – a_pradhan Feb 11 '15 at 16:12
  • @AkashPradhan are you trying to suggest what happens for imaginary numbers? – ha9u63a7 Feb 11 '15 at 16:14
  • @ControlAltDel: I wrote a code that collects the coefficients, but this question specified that we had to take in a quadratic equation as a whole and output the solution. – stealingbikes Feb 11 '15 at 16:16
  • Aside from your question, you shouldn't compare `x == 0` like that. Doubles often get rounded, so it is possible that you will get a value that "should be" 0, but isn't equal to 0. You should check the distance of x to 0 against a small threshold instead (e.g. `Math.abs(x) < 0.0001`) – mhlz Feb 11 '15 at 16:26
  • For real coefficients, the roots can still be complex (non-real and conjugate) and for a1 == 0, your equation boils down to a linear one. In the first case, you will be doing sqrt of a negative number and in the second, division by zero - both of which will generate exceptions. – a_pradhan Feb 11 '15 at 16:45

1 Answers1

2

You want to use regular expressions to parse the command line input. It seems that what you're trying to do has been done many times before.

See here

Community
  • 1
  • 1
Richard
  • 1,070
  • 9
  • 22
  • Hi Richard, if it has been done many times before (and you even link to an SO question), consider flagging the question as duplicate rather than adding a link to the duplicate as an answer. Especially if your own answer is so short. Right now, your answer appeared in the "low-quality" queue, so remember that for the future. – Oliver W. Feb 11 '15 at 17:46
  • I've only just really got involved here, so I'm not sure I have rights to mark as duplicates. I'll bear it in mind for the future. – Richard Feb 11 '15 at 17:51
  • It seemed to me that the problem this person was having was not related to quadratic equations, but parsing command line input accurately. The solution to that would seem to be regular expressions. I directed them to a place that had an answer because that seemed helpful. Next time I will do it as a comment. – Richard Feb 11 '15 at 23:48