Thomas Teixeira
Thomas Teixeira

Reputation: 11

Scanner Syntax Error

I'm a beginner in Java, and I'm using the Eclipse.

I'm trying to build a very simple program that takes two numbers from input and returns the sum. But I'm having a syntax error in the Scanner and can't realize what it is (error on comment):

import java.util.Scanner;
public class soma {
    int n1, n2, soma;
    Scanner sc1 = new Scanner(System.in); // syntax error on token ";", { expected after this token
    n1 = sc1.nextInt();
    Scanner sc2 = new Scanner(System.in);
    n2 = sc2.nextInt();
    soma = n1 + n2;
    System.out.println("A Soma de " + n1 + " e " + n2 + " é: " + soma);

} // Syntax error, insert "{" to complete ClassBody

Upvotes: 0

Views: 718

Answers (1)

chiastic-security
chiastic-security

Reputation: 20520

You need to find a tutorial on Java and follow it closely.

You're confusing a class and a method. A class contains methods, and a method contains code to run. You should move the executable code into a method inside your class.

If you then want to run your program, you'll need a public static void main(String args[]) method. This will create an instance of your soma class, and invoke a method on it.

This will all be covered in a Java basics tutorial or book.

Upvotes: 3

Related Questions