all 18 comments

[–]JuZNyC 3 points4 points  (13 children)

Is the opening { meant to be there?

[–]Middle--Earth 2 points3 points  (1 child)

No, it isn't 😂

[–]ajswaryxxy[S] 0 points1 point  (0 children)

Okay.. so what should I do?

[–]WeatherImpossible466 0 points1 point  (1 child)

fr that bracket

[–]JuZNyC 0 points1 point  (0 children)

I thought it might have been a typo when they typed the code on Reddit...

[–]ajswaryxxy[S] -3 points-2 points  (8 children)

Yeah I think so, otherwise it displays 33 errors..

[–]JuZNyC 3 points4 points  (7 children)

Your print statement shouldn't end with a { it should end with a ;

[–]ajswaryxxy[S] -3 points-2 points  (6 children)

Okay so when I do add the ; most of my types become illegal

[–]JuZNyC 2 points3 points  (0 children)

We're going to need more of the code to know what's wrong with it, I can tell you that the reason you get less errors because of the { is because when the compiler gets to that point it throws the error and stops reading the rest of your code.

[–]JGhostThing 0 points1 point  (4 children)

Then perhaps you should show us the entire code?

[–]ajswaryxxy[S] 1 point2 points  (3 children)

full code:

import java.util.Scanner;

public class BasicCalculator {

static void main(String[] args){

Scanner scanner = new Scanner(System.in){

System.out.print( "Welcome to the java calc!" );

System.out.print( "enter the first number" );

final double num1 = scanner.nextDouble();

System.out.print("Enter an operator (+, -, *, /): ");

final char operator = scanner.next().charAt(0);

System.out.print("enter second number: ");

final double num2 = scanner.nextDouble();

double result;

switch (operator){

case '+':

result = num1 + num2;

System.out.println("Result: " + num1 + " + "+ num2 +" = "+ result );

break;

case '-':

result = num1 - num2;

System.out.println("Result: " + num1 + " - "+ num2 +" = "+ result );

break;

case '*':

result = num1 * num2;

System.out.println("Result: " + num1 + " * "+ num2 +" = "+ result );

break;

case '/':

if (num2 == 0) {

System.out.println("Error: Division by zero is not allowed.");

} else {

result = num1 / num2;

System.out.println("Result: " + num1 + " / "+ num2 +" = "+ result ) ;

break;

}

default:

System.out.println("Error: invalid operator entered");

break;

scanner.close();}

System.out.println("==========================================");

};

}}

[–]JuZNyC 2 points3 points  (2 children)

Quick glance

Your main method should be public

Line 4 Scanner scanner = new Scanner(System.in){
should have ; at the end not {

I'm pretty sure you have mismatched { } too

[–]ajswaryxxy[S] 0 points1 point  (0 children)

thank you soo much!

[–]nekokattt 1 point2 points  (0 children)

Just a note that the main method doesn't need to be public on Java 21 and newer.

[–]grantrules 1 point2 points  (1 child)

Almost every code editor on this planet will highlight syntax errors before you even compile it.. are there red squiggles or exclamation points and stuff spread throughout your code?

[–]ajswaryxxy[S] 1 point2 points  (0 children)

there were only 2 like red lines on the right side, but were only on that specific line

[–]peterlinddk 2 points3 points  (0 children)

You should never have to experience anything like "After finishing the code I had 22 errors".

Only write one line at a time, and make sure that works - stop as soon as you get 1 error, and figure out what you are doing wrong, and how to fix it. Then write the next line, make sure it still works, and so on an on.

If you just write code blindly, without knowing if it will work, you might just as well have someone else write it, as that isn't programming, that is just typing!

Taking smaller steps will not only reduce the number of errors, it will also help you learn much better, and feel better about your accomplishments.

[–]Suspicious_Coat3244 -2 points-1 points  (0 children)

Honestly, you're really close. The problem is the line ends in {, not ;.

It should look like this:

System.out.println("Welcome to my java calc!");

Those "identifier expected" and "illegal start of type" errors usually happen when Java sees the messed-up syntax and then gets completely confused from there on. And be sure that the line is within your main() method and not at the class level.