0

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



}

Edited by happygeek: fixed formatting

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by javaAddict
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.