Home java Compilation error: illegal start of expression

Compilation error: illegal start of expression

Author

Date

Category

An error occurs while creating:

(9: 9) illegal start of expression.

What’s my mistake?

Code available:

package com.company;
import java.util.Scanner;
 public class Main {
public static void main (String [] args) {
  // write your code here
  Scanner scc = new Scanner (System.in);
  public static void main (String [] args) {
    int firstNum = whatnumber ();
    int secodNum = whatnumber ();
    char znak = Goperation ();
    int resault = resaultX ();
    System.out.print (resault);
  }
  public static int whatnumber () {
    System.out.print ("Enter a number:");
    int num;
    num = scc.nextInt ();
  }
  public static void Goperation () {
    System.out.print ("Enter a sign:");
    char znakL;
    znakL = scc.hasNext ();
  }
  public static void resaultX (int firstNum, int secondNum, char operation) {
    int resault;
    switch (operation) {
      case "+":
        resault = firstNum + secondNum;
        break;
      case "-":
        resault = firstNum - secondNum;
        break;
      case "/":
        resault = firstNum / secondNum;
        break;
      case "*":
        resault = firstNum * secondNum;
        break;
    }
  }
}

Answer 1, authority 100%

  1. As said main inside main – you cannot write a method inside a method
  2. It’s not clear at all you need this (second line from main) – just remove
  3. Even though you misspelled main a second time – you haven’t closed the parenthesis here yet, you should always close opening parentheses.
  4. In the Goperation () method – scc.hasNext () – returns boolean – true / not true, i.e. … you are not writing a charm, but checking for availability.
    “There is also a hasNext () method that checks if there are any characters left in the input stream.”
    http://kostin.ws/java/java-input-stream.html

    1. If you use char – then you need to specify '' , instead of "" – they are used for String .

Answer 2

package My.Package;
import java.util.Scanner;
public class Main {
  Scanner scc = new Scanner (System.in);
  public static void main (String [] args) {
    int firstNum = whatnumber ();
    int secodNum = whatnumber ();
    String znak = Goperation ();
    int resault = resaultX (firstNum, secodNum, znak);
    System.out.print (resault);
  }
  public static int whatnumber () {
    Scanner scc = new Scanner (System.in);
    System.out.print ("Enter a number:");
    int num;
    num = scc.nextInt ();
    return num;
  }
  public static String Goperation () {
    Scanner scc = new Scanner (System.in);
    System.out.print ("Enter a sign:");
    String znakL;
    znakL = scc.nextLine ();
    return znakL;
  }
  public static int resaultX (int firstNum, int secondNum, String operation) {
    int resault;
    switch (operation) {
      case "+":
        resault = firstNum + secondNum;
        break;
      case "-":
resault = firstNum - secondNum;
        break;
      case "/":
        resault = firstNum / secondNum;
        break;
      case "*":
        resault = firstNum * secondNum;
        break;
      default: resault = 0;
    }
    return resault;
  }
}

Answer 3

(9: 9) illegal start of expression. – says that this is a compilation error. The structure of a Java program has a defined syntax . This syntax defines the rules for using language elements in your program.

Java is a very complex language. Of course, reading JLS will be difficult, so Beginners are encouraged to start with a basic understanding. Then go on to coding.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions