-3

I have a trouble with my code . I think the problem is about delims=[+,-,*,/]+. when i write if (delims.equals("[+]+")) for ex, it takes only [+]. however delims is not equal just [+]. Ithink you got what i mean. delims is equal [+,-,*,/]+.

public static void main(String[] args) {

        System.out.println("Please enter your calculation");
        Scanner sc = new Scanner(System.in);
       String s=sc.next();
       String delims="[+,-,*,/]+";
       String[] tokens=s.split(delims);


       for(int i=0; i<1; i++){
        String s1=tokens[i];
        for (int j=1; j<2; j++){
        String s2=tokens[j];


      double n1=Double.parseDouble(s1);
      double n2=Double.parseDouble(s2);


      if (delims.equals("[+]+")){
        System.out.println(n1+n2);
    System.exit(0); }

        if (delims.equals("[-]+")){
            System.out.println(n1-n2);
        System.exit(0);}

        if (delims.equals("[*]+")){
                System.out.println(n1*n2);
        System.exit(0);}

                if (delims.equals("[/]+")){
                System.out.println(n1/n2);
                System.exit(0);
                }
}}}}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Lala Ali
  • 1
  • 4
  • See [this](http://stackoverflow.com/questions/5993779/java-use-split-with-multiple-delimiters) question. You have to tell it what to split the string on. Also it seems you're trying to use regex without telling it that you want to search the string with regex... – Kidiskidvogingogin Mar 26 '16 at 21:25
  • You probably wanted to say if (s2.equals("[+]+"))... In your code you are comparing delims with "[+]+", but delims is always "[+,-,*,/]+". – Milan Mar 26 '16 at 21:27
  • So what is your solution?) – Lala Ali Mar 26 '16 at 21:32
  • @Milan do u have a solution?) – Lala Ali Mar 26 '16 at 21:59
  • Please do **not** deface your own question. Question rolled back. – Hovercraft Full Of Eels Mar 27 '16 at 10:23
  • I have to do it because this is my assignment and if I dont do it it will be considered as cheating. please delete this question – Lala Ali Mar 27 '16 at 10:45
  • Please stop defacing your question. If you got help on this site, then it should be publicly view-able by all, including your teachers. The help on this site is not just for you, the questioner, but for all who need similar help in the future, and if you deface your question, you prevent future visitors from getting this help. If you're not allowed to get help on this and other sites, then you shouldn't do so, but regardless, please don't deface the question. – Hovercraft Full Of Eels Mar 27 '16 at 11:30
  • please try to understand me and please delete thsi question I am so sorry In understood what you meant and what is your policy but try to undertand me in this case please – Lala Ali Mar 27 '16 at 11:37

1 Answers1

1

The delimiter is consumed (thrown away), but you need it.

Try this instead:

String[] tokens = s.replace(" ","").split("\\b");

\b means "word boundary", and digits are considered word characters, so this will work when the number parts of the input are whole numbers.

I added a call to `replace' to remove all spaces.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • numberFormatException( – Lala Ali Mar 26 '16 at 21:38
  • @lala depends what your input is, but I would expect every even indexed token to be a number and every odd one an operator. Try this to see how it split: `Arrays.stream( tokens).forEach(System.out::println);` – Bohemian Mar 26 '16 at 21:41
  • it doesnot work. I want to enter input like this: 12+5 or 9/4 . my problem is delims is multiple and when I wrote for ex if (delims.equals("[+]+) it will take just [+] and desnt match with delims which is multiple – Lala Ali Mar 26 '16 at 21:52
  • @lala this is the solution. What's the problem? If you input is `"12+5"` tokens will be `["12", "+", "5"]`. Write some code to deal with that. Hint: your code won't work at all. Start again with something a lot simpler. – Bohemian Mar 26 '16 at 22:14
  • @lala yes. You don't need nested loops. You need to consume the tokens array two elements at a time, except the first which us your starting value for the result. Do it with pen and paper first, then write your code. – Bohemian Mar 26 '16 at 22:50
  • @lala use `double num = Double.parseDouble(tokens[0]);` – Bohemian Mar 27 '16 at 00:27
  • @Bohemian I think you will understand me I have to delete this question because of cheating issues – Lala Ali Mar 27 '16 at 11:42