-1

I am working on a linear system solver and I would like the user to be able to input the whole equation in one go as oppose to separately inputting each number.

I have tried to split the string at the "+" and "=" (The equation format is (ax+by=c)) but it is splitting the second part after the plus as "by=c" rather than just "by".

Here is my code:

System.out.print("Enter Your First Equation (ax + by = c): " );
String eq1 = sc.nextLine();

String[] part = eq1.split("\\+");
String[] part2 = eq1.split("\\=");

String result = part[0];

String result2 = part[1];

String result3 = part2[1];


double a1 = Double.parseDouble(result);
double b1 = Double.parseDouble(result2);
double c1 = Double.parseDouble(result3);

System.out.print("Enter Your Second Equation (ax + by = c): " );
String eq2 = sc.nextLine();

String[] part3 = eq1.split("\\+");
String[] part4 = eq1.split("\\=");

String result4 = part3[0];

String result5 = part3[1];

String result6 = part4[1];

double a2 = Double.parseDouble(result4);
double b2 = Double.parseDouble(result5);
double c2 = Double.parseDouble(result6);


System.out.println("Your Equations Are (1) " + a1 + "x + " + b1 + "y = " + c1);
System.out.println("                   (2) " + a2 + "x + " + b2 + "y = " + c2);
nbrooks
  • 18,126
  • 5
  • 54
  • 66
Herseept72
  • 25
  • 1
  • 6
  • 1
    Possible duplicate of [Java: use split() with multiple delimiters](http://stackoverflow.com/questions/5993779/java-use-split-with-multiple-delimiters) – nbrooks Nov 07 '16 at 02:44
  • `split` is good for splitting a string up based on delimiters, when you don't care about the exact form of the delimiters. Here, you do care, because I assume you will treat `+` and `=` differently. Using a loop where you parse the next token off the string with a regex would be better. `split` is probably not the right tool for this job. – ajb Nov 07 '16 at 02:48
  • Here, though, if there will not be any operators to the right of `=`, you could almost get away with `split` by splitting on `=` _first_, and then splitting _only the first element_ (the part to the left of `=`) on `+`. But I have to ask: how do you intend to handle negative coefficients? Do we have to type in `2x+-3y=7`? Yuck. – ajb Nov 07 '16 at 02:50
  • The 2nd `split` looks for q backslash and an equals sign. `=` has no special meaning in a regular expression, so don't escape it. – Robert Nov 07 '16 at 02:53
  • @Robert No, the second `split` doesn't look for a backslash. There's only one backslash in the pattern string, and to look for a backslash, you need two backslashes in the string (which means four backslashes in the string literal). You can escape any character in a regex that isn't a letter or digit, even if the escape is unnecessary. – ajb Nov 07 '16 at 03:46

2 Answers2

1

String.split won't give you the desired result, which I guess would be "ax", "by", since it would have to look for a token to split at and would strip that token.

Try Pattern and Matcher instead, using this regex: (\d*)([a-zA-Z]+)|(\d+).

e.g:

public static void main(String[] args) {

        String exp = "53x+6y=30";

        String pattern = "(\\d*)([a-zA-Z]+)|(\\d+)";

        // Create a Pattern object
          Pattern r = Pattern.compile(pattern);

          // Now create matcher object.
          Matcher m = r.matcher(exp);

          while (m.find( )) {
             System.out.println("Found value: " + m.group(0) );
          }
    }

Output -

Found value: 53x
Found value: 6y
Found value: 30
Khuzi
  • 2,792
  • 3
  • 15
  • 26
0

If you insist on using the String.split() method then I suppose you could do it like this (just as an example):

String eq = "ax+by=c";
String[] splitStrg1 = eq.split("=");
String[] splitStrg2 = splitStrg1[0].split("\\+");
String part1 = splitStrg2[0];
String part2 = splitStrg2[1];
String part3 = splitStrg1[1];

System.out.println("Part 1 is: " + part1);
System.out.println("Part 2 is: " + part2);
System.out.println("Part 3 is: " + part3);
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22