i am making a simple calculater , and know think i doing correctly but when i compile it it has error like this
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert "}" to complete SwitchBlock
Syntax error, insert "}" to complete Block
Syntax error, insert "}" to complete Block
and i trying to check everything but it still not work . so what wrong with it anyone know about this? please help thank.
my codeg
import java.util.Scanner;
public class demo {
public static void main(String[] args)
{
try {
Scanner sc=new Scanner(System.in);
System.out.println("Calculator is on ");
double result=0.0;
System.out.println("Result: " + result);
boolean flag=true;
while(flag){
String line=sc.nextLine();
char ch=line.charAt(0);
switch(ch){
case '+':
result+=Integer.parseInt(line.substring(1));
System.out.println("Result "+line +" = " +result);
System.out.println("New Result = "+result);
break;
case '-':
result-=Integer.parseInt(line.substring(1));
System.out.println("Result "+line +" = " +result);
System.out.println("New Result= "+result);
break;
case '*':
result*=Integer.parseInt(line.substring(1));
System.out.println("Result "+line +" = " +result);
System.out.println("New Result= "+result);
break;
case '/':
result/=Integer.parseInt(line.substring(1));
System.out.println("Result "+line +" = " +result);
System.out.println("New Result = "+result);
break;
default:
if(flag=false)
throw new Exception();
System.out.println(ch+" Is an unknown Operator ");
}
catch (Exception ex) {
System.out.println(ex.getMessage());
secondChance();
}
}
public static void secondChance(){
Scanner sc=new Scanner(System.in);
System.out.println("Calculator is on ");
double result=0.0;
System.out.println("Result: " + result);
boolean flag=true;
while(flag){
String line=sc.nextLine();
char ch=line.charAt(0);
switch(ch){
case '+':
result+=Integer.parseInt(line.substring(1));
System.out.println("Result "+line +" = " +result);
System.out.println("New Result = "+result);
break;
case '-':
result-=Integer.parseInt(line.substring(1));
System.out.println("Result "+line +" = " +result);
System.out.println("New Result= "+result);
break;
case '*':
result*=Integer.parseInt(line.substring(1));
System.out.println("Result "+line +" = " +result);
System.out.println("New Result= "+result);
break;
case '/':
result/=Integer.parseInt(line.substring(1));
System.out.println("Result "+line +" = " +result);
System.out.println("New Result = "+result);
break;
}
}
}
}