Basic Java calc errors help
Posted by ajswaryxxy@reddit | learnprogramming | View on Reddit | 17 comments
So I started my second code today and I tried doing the basic calculator. After finishing the code I had 22 errors, which I eliminated however I keep getting “
I’m new to Java and almost new to coding itself.
Line 8:
System.out.println( “Welcome to my java calc!” ){
peterlinddk@reddit
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!
Take smaller steps.
Suspicious_Coat3244@reddit
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.
JuZNyC@reddit
Is the opening { meant to be there?
ajswaryxxy@reddit (OP)
Yeah I think so, otherwise it displays 33 errors..
JuZNyC@reddit
Your print statement shouldn't end with a { it should end with a ;
ajswaryxxy@reddit (OP)
Okay so when I do add the ; most of my types become illegal
JGhostThing@reddit
Then perhaps you should show us the entire code?
ajswaryxxy@reddit (OP)
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@reddit
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@reddit (OP)
thank you soo much!
JuZNyC@reddit
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.
WeatherImpossible466@reddit
fr that bracket
JuZNyC@reddit
I thought it might have been a typo when they typed the code on Reddit...
Middle--Earth@reddit
No, it isn't 😂
ajswaryxxy@reddit (OP)
Okay.. so what should I do?
grantrules@reddit
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@reddit (OP)
there were only 2 like red lines on the right side, but were only on that specific line