0

I am new to Java and I want to solve a simple problem in java.

In input I need to take an integer a and then a character c and then another integer b And print the output if the character is '+' then print a+b And so on like this.

The input looks like : 6+4

But I find an error continuously like this

Exception in thread "main" java.util.InputMismatchException

My Code:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Scanner sc1 = new Scanner(System.in);
    
    try {
        int a, b, ans = 0;
        char c;
        
        a = sc.nextInt();
        c = sc1.next().charAt(0);
        b = sc.nextInt();

        if (c=='+') {
            ans = a + b;
        } else if (c=='-') {
            ans = a - b;
        } else if (c=='*') {
            ans = a * b;
        } else if (c=='/') {
            ans = a / b;
        }

        System.out.println(ans);
    } finally {
        sc.close();
        sc1.close();
    }
}
CSE
  • 73
  • 6
  • First up, you just need one `Scanner`, not two. Secondly, _exactly_ what input are you providing? Your program will only work if you have spaces between everything, for example `2 + 3`, not `2+3`. – Dawood ibn Kareem Aug 16 '21 at 05:33
  • @DawoodibnKareem I need to take `2+3` input like this – CSE Aug 16 '21 at 05:37
  • Then you'll probably need to read it into a single `String` then go looking for the part that isn't a number. It's possible with a `Scanner` but kind of awkward. – Dawood ibn Kareem Aug 16 '21 at 05:39
  • @DawoodibnKareem if you don't mind will you provide the answer. Since I am new and so I have less Idea about java :( – CSE Aug 16 '21 at 05:41
  • as @DawoodibnKareem said next() and nextInt() keep reading till it count the first white space (space - tab - new line - so any thing that classified as space ) –  Aug 16 '21 at 06:01
  • many many thanks. both of you :) – CSE Aug 16 '21 at 06:19
  • Does this answer your question? [Is there an eval() function in Java?](https://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java) – TYeung Aug 16 '21 at 11:49
  • @LearningMathematics I think this is not :( – CSE Aug 17 '21 at 04:01

1 Answers1

0

remove the double scanner object and try this

Scanner sc = new Scanner(System.in);

    int a, b, ans = 0;
    String c;
    String input = sc.nextLine();
    Pattern pattern = Pattern.compile("([0-9]+)([+*/-])([0-9]+)");
    Matcher matcher = pattern.matcher(input);
    if (!matcher.find()) {
        System.out.println("Invalid command");
    } else {
        a = Integer.valueOf(matcher.group(1));
        c = matcher.group(2);
        b = Integer.valueOf(matcher.group(3));

        if (c.equals("+")) {
            ans = a + b;
        } else if (c.equals( "-")) {
            ans = a - b;
        } else if (c.equals( "*")) {
            ans = a * b;
        } else if (c.equals("/")) {
            ans = a / b;
        }

        System.out.println(ans);
    }
  • This works perfectly.But can you explain that why we use `charAt(1)` in char and `charAt(2)` in int value? – CSE Aug 16 '21 at 06:21
  • for example 1+2 is the input so the first char will be 1 so input.charAt(0) will be for first number and 1 for the sign and 2 for the 2nd number Character.getNumericValue used to get the int value of that char as charAt return char and if you used it directly like this a = input.charAt(0) i will make a equal the unit code value of whatever exist at 0 which we do not want try to use this and see the difference –  Aug 16 '21 at 06:26
  • if I put `7+54` then the output should be `61` but is shows `12` :( how to solve this? – CSE Aug 16 '21 at 06:29
  • try to use regex as the updated version that i just did check my answer again –  Aug 16 '21 at 06:51
  • i use jdk 11 and intelljidea ide did you used the new answer and what the output you get –  Aug 16 '21 at 06:55
  • Thanks :) I see your edited code and now it's work for me perfectly :) thanks . +1 for this solution :) – CSE Aug 16 '21 at 06:58