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%
- As said main inside main – you cannot write a method inside a method
- It’s not clear at all you need this (second line from main) – just remove
- Even though you misspelled main a second time – you haven’t closed the parenthesis here yet, you should always close opening parentheses.
-
In the
Goperation ()
method –scc.hasNext ()
– returnsboolean
– true / not true, i.e. … you are not writing a charm, but checking for availability.
“There is also ahasNext ()
method that checks if there are any characters left in the input stream.”
http://kostin.ws/java/java-input-stream.html- If you use char – then you need to specify
''
, instead of""
– they are used forString
.
- If you use char – then you need to specify
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.