0

First off, I will admit that this is an assignment of mine. however, I am at my wits end. I tried need to validate that the user input a proper expression (ie: "7 + 5;") and I managed to do it with split methods but I was told that I can't do that. I'm sure there is a simple solution to the problem but I am not seeing it.

The code is rather lengthy so I won't post it but if I will if needed.

Thanks!

Edit to answer questions: I am writing in jGrasp, so they can do whatever is on the keyboard. I was told to "Find a creative way to use substrings" which I don't know what that means. The expression needs to be Number Space operand Space number semicolon

here is what I have for the validation... I am using arrays for each character in the expression

    public static boolean validFormat(String expr)
    {
  String tokens[] = expr.substring()
  if (tokens.length == 3)
  {
     if (tokens[0].equals(""))
     {
        return false;
     }
     if (tokens[1].equals(""))
     {
        return false;
     }
     if (tokens[2].length < 2)
     {
        return false;
     }
         else
         {
         if (tokens[2].endwith(";"));
         {
           return false;
         }
         else
           {
           return true;
           }
         }
     }
    return false;

}

I get an error with calling the substring as well as an "else without if" error

Isaac Bly
  • 1
  • 1
  • "*I managed to do it with split methods but I was told that I can't do that.*" so what can you use? – Pshemo Oct 25 '15 at 01:21
  • How are you wanting to do this? Using a regex? How are they inputting it? You could force them to use a GUI where they can only press the calculator buttons? – Alex Oct 25 '15 at 01:21
  • define a proper expression more clearly please. For example, are the spaces needed? Are integers and operators all that can be accepted input? – tale852150 Oct 25 '15 at 01:24
  • [`Java substring`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)) – sam Oct 25 '15 at 01:34
  • Possible duplicate of [Evaluating a math expression given in string form](http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form) – trooper Oct 25 '15 at 01:37
  • The most upvoted answer actually isn't much help, but the second most upvoted has a good example of a Parser implementation that does this. – trooper Oct 25 '15 at 01:40

2 Answers2

0

First, you should limits the input to 6 characters using an if statement. Then use the CharAt() method to return each character to check the condition.

Wesley.C
  • 75
  • 6
0

I was told to "Find a creative way to use substrings".

As told, try using String.substring() for the same.

public class Demo {

    public static void main (String[] args) {
      String exp = "7 + 5;";
      System.out.printf("%s\t%b%n", exp, validate(exp));
      exp = "4 + d;";
      System.out.printf("%s\t%b%n", exp, validate(exp));
    }

    public static boolean validate(String exp) {
      String n1 = exp.substring(0,1);//operand
      String n2 = exp.substring(4,5);//operand
      String co = exp.substring(5);//semicolon
      String s1 = exp.substring(1,2);//space
      String s2 = exp.substring(3,4);//space
      String op = exp.substring(2,3);//operator
      return num(n1) && num(n2) && semi(co) && space(s1) && space(s2) && opt(op);
    }

    public static boolean num(String n) {
      return "0123456789".contains(n);
    }

    public static boolean semi(String s) {
      return ";".equals(s);
    }

    public static boolean space(String s) {
      return " ".equals(s);
    }

    public static boolean opt(String s) {
      return "-+*%/^".contains(s);
    }
}

This solution uses RegExp:

public class Demo {

    public static void main (String[] args) {
      String exp = "7 + 5;";
      System.out.printf("%s\t%b%n", exp, validate(exp));
      exp = "4 + d;";
      System.out.printf("%s\t%b%n", exp, validate(exp));
    }

    public static boolean validate(String exp) {
      return exp.matches("\\d\\s(\\+|\\-|\\*|\\/|\\^|\\%)\\s\\d\\;");
    }
}