T4yl0r T4yl0r - 5 months ago 33
Java Question

Syntax error on token ";", , expected on Scanner object

The code is the following:

import java.util.Scanner;
public class Programm2 {
int n;
Scanner inp = new Scanner(System.in);
n = inp.nextInt();
}


All that I'm trying to get is user input and I don't know why get this error on to the inp's declaration line.
My IDE is Eclipse and the class is created with the default settings(nothing new added to it).

Answer

Your code needs to go inside a method.

class Programm2
{

   int n = 0;

   public void aMethod() {
      Scanner inp = new Scanner(System.in);
      n  = inp.nextInt(); 
   }
}