1 / 2
Oct 2013

I am solving a problem which should perform addition,multiplication or subtraction on the basis of + - or * encountered in input.
Input is in the form 67+23 or 34-14 etc. So how can i check - + or * present in the input to perform the specific task.I have done so far..

import java.util.Scanner;
import java.io.IOException;
public class Summ{
public static void main(String[] args){
int a,b;
Scanner sc=new Scanner(System.in);
sc.useDelimiter("[\s+,*-]+");
a=sc.nextInt();
b=sc.nextInt();
int sum=a+b;
int diff=a-b;
int mul=a*b;
if(+ sign encountered?????){
System.out.println(" "+a);
System.out.println("+"+b);
System.out.println("_ _ _ _");
System.out.println(sum);
}
else if(- sign encountered){
System.out.println(" "+a);
System.out.println("-"+b);
System.out.println("_ _ _ _");
System.out.println(diff);
}
else if(* sign encountered){
System.out.println(" "+a);
System.out.println("*"+b);
System.out.println("_ _ _ _");
System.out.println(mul);
}

}

}

  • created

    Oct '13
  • last reply

    Oct '13
  • 1

    reply

  • 297

    views

  • 2

    users

Iterate character by character through the string. If you use the symbols as delimiters then you don't get to see what they are.

Suggested Topics

Want to read more? Browse other topics in JAVA based languages or view latest topics.