An error occurs when creating: (9: 9) illegal start of expression.

Just learning, the first week. What's my mistake?

there is a code:

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("Введите число: "); int num; num = scc.nextInt(); } public static void Goperation() { System.out.print("Введите знак: "); 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; } } } 
  • You cannot declare methods inside methods, and in your main one more main and a bunch of other methods are crammed. - Sergey Gornostaev
  • You have all a lot of errors, you found the code on the Internet and do not understand it? Write your. First write the number input, run, check, debug, display the numbers on the screen. Then when everything will work as it should write the input of the sign, etc. - Komdosh
  • one
    @SergeyGornostaev, at first I also thought that this was the only mistake, but it turned out that almost everything was wrong. The input of the sign is incorrect, the comparison with the symbol in the switch , the incorrect return value of the Goperation , in general, is a lot. - Komdosh
  • Just learning, the first week. I could be wrong or make mistakes, thanks. You helped, I just got nervous. Your advice helps more constructively than what I did myself. Thank you so much. NEW bugs came out, but they are new. - Shurok Petrov
  • @ ShurokPetrov what textbook do you study? - Sergey Gornostaev

3 answers 3

  1. As said main inside main - you cannot write a method inside a method
  2. It is not clear what you need at all (second line with main) - just delete
  3. Even considering that you incorrectly wrote the second time main - here you have not yet closed the bracket, always opening brackets should be closed.
  4. In the Goperation() method - scc.hasNext() - returns a boolean - true / not true, i.e. You do not write charms, but check availability. "There is a hasNext() method that checks if any characters remain 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 .
      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("Введите число: "); int num; num = scc.nextInt(); return num; } public static String Goperation() { Scanner scc = new Scanner(System.in); System.out.print("Введите знак: "); 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; } } 

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

      Initially, you were probably taught how to create a program in the Java language, but you probably missed it, because you not only make coding errors, but also write the code incorrectly. These are different things that you need to understand before continuing to encode in Java.

      Java is a very complex language, and you will understand this some day by looking at the description of syntax. Of course, reading JLS immediately will be difficult, so beginners are recommended to begin with an understanding of basic concepts. Then go to the coding.