user3241004
user3241004

Reputation: 24

JAVA ERROR .need suggestion in form of comments or codes if possible pls,

as i am newbie.i have tried some code..pls suggeesst some problem.. am getting this error.pls help me solve this problem..

import java.util.Scanner;
import java.io.*;
package calculator;

public class calc {
    public static void main(String[] args) {
    }

            private int valueA;
            private int valueB;
            private String operator;
            private char operatorA;

            public int getvalueA() {
            return valueA;
            }

            public int getvalueB() {
            return valueB;
            }

            {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter problem.");

            {

            valueA = keyboard.nextInt();
            valueB = keyboard.nextInt();
            operator = keyboard.next();
            operatorA = operator.charAt(0);

            int add = valueA + valueB;
            int minus = valueA - valueB;
            int multiply = valueA * valueB;
            int divide = valueA / valueB;
            String clearScreen = null;


            switch (operatorA) {
            case '+':
            System.out.println(add);
            break;

            case '-':
            System.out.println(minus);
            break;

            case '*':
            System.out.println(multiply);
            break;

            case '/':
            System.out.println(divide);

            case 'C':
            System.out.println(clearScreen);
            break;

            default:
            System.out.println("unknown operator '" + operator + "'. Please try again.");
            break;
            }

            }
        }

pls provide corrective points???

: Syntax error, insert "}" to complete MethodBody

Upvotes: 0

Views: 66

Answers (3)

Martin Dinov
Martin Dinov

Reputation: 8825

The main issue the compiler was complaining about was the incorrect placement of braces (one was missing at the end). However, you probably didn't write the code in the right place either. You probably wanted to have most of it in main(), but instead you had it in the constructor and so it won't run without at least instantiating an object of type Calc. Really, you want to refactor your code to something like this:

import java.util.Scanner;

public class calc {
    private static int valueA;
    private static int valueB;
    private static String operator;
    private static char operatorA;

    public int getValueA() {
        return valueA;
    }

    public int getValueB() {
        return valueB;
    }

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter problem.");

        valueA = keyboard.nextInt();
        valueB = keyboard.nextInt();
        operator = keyboard.next();
        operatorA = operator.charAt(0);

        int add = valueA + valueB;
        int minus = valueA - valueB;
        int multiply = valueA * valueB;
        int divide = valueA / valueB;
        String clearScreen = null;

        switch (operatorA) {
        case '+':
            System.out.println(add);
            break;

        case '-':
            System.out.println(minus);
            break;

        case '*':
            System.out.println(multiply);
            break;

        case '/':
            System.out.println(divide);

        case 'C':
            System.out.println(clearScreen);
            break;

        default:
            System.out.println("unknown operator '" + operator
                    + "'. Please try again.");
            break;
        }

    }

}

That is, if you want to access the variables in the class from main, they will have to be declared as static (as main is/has to be static) as I have done for you here.

Alternatively, you could instantiate an object of type Calc and access its members via the getters you've created and then the variables do not have to be static (as you perhaps originally intended). This is generally cleaner and I'll leave that to you as an exercise. Note the standard naming conventions in Java: camelCase and class names start with a capital letter.

Upvotes: 4

Aurélien Thieriot
Aurélien Thieriot

Reputation: 5923

You have some weird curly brackets placement.

Here what should be ok:

import java.util.Scanner;
import java.io.*;
package calculator;

public class calc {

    private int valueA;
    private int valueB;
    private String operator;

    private char operatorA;

    public int getvalueA() {
       return valueA;
    }

    public int getvalueB() {
       return valueB;
    }

    public static void main(String[] args) {

            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter problem.");

            valueA = keyboard.nextInt();
            valueB = keyboard.nextInt();
            operator = keyboard.next();
            operatorA = operator.charAt(0);

            int add = valueA + valueB;
            int minus = valueA - valueB;
            int multiply = valueA * valueB;
            int divide = valueA / valueB;
            String clearScreen = null;


            switch (operatorA) {
               case '+':
               System.out.println(add);
               break;

               case '-':
               System.out.println(minus);
               break;

               case '*':
               System.out.println(multiply);
               break;

               case '/':
               System.out.println(divide);

               case 'C':
               System.out.println(clearScreen);
               break;

               default:
               System.out.println("unknown operator '" + operator + "'. Please try again.");
               break;    
            }
     }
  }

Upvotes: 0

erzr2
erzr2

Reputation: 165

Make sure all your braces match up with methods. Your code to run it also needs to be a method.

    public static void parse{
         Scanner keyboard = new Scanner(System.in);
         System.out.println("Enter problem.");
             valueA = keyboard.nextInt();
             valueB = keyboard.nextInt();
          operator = keyboard.next();
          operatorA = operator.charAt(0);

          int add = valueA + valueB;
          int minus = valueA - valueB;
          int multiply = valueA * valueB;
          int divide = valueA / valueB;
          String clearScreen = null;


          switch (operatorA) {
          case '+':
          System.out.println(add);
          break;

          case '-':
          System.out.println(minus);
          break;

          case '*':
          System.out.println(multiply);
          break;

          case '/':
          System.out.println(divide);

          case 'C':
          System.out.println(clearScreen);
          break;

           default:
           System.out.println("unknown operator '" + operator + "'. Please try     again.");
          break;
           }

then call the method from your your main method.

Upvotes: 0

Related Questions