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;
}
}
}



}

It is obvious that you need to count the brackets.
I don't know how you usually write code, but do not open anything without closing it first.
You need to open an if () { } ?
First write this:

if () {
  
}

And then go and add what you want in the () and the {}. If you need more things in the if then write for example:
if (()&&()) {
for (;;) {

}
}
And THEN write what you want in the the brackets

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.