Steve
Steve

Reputation: 81

Scanner Object Error

I don't understand why I always get an error whenever I create a new object from the Scanner class.

I have JDK 1.8.0.25

import java.util.Scanner;

public static Scanner input = new Scanner (System.in);



public class NewClass {
    public static void main(String args[]) {
        System.out.print("Hello");
    }

}

Upvotes: 1

Views: 222

Answers (3)

Angelo Alvisi
Angelo Alvisi

Reputation: 479

It seems to me you are trying to write java using a Text editor. My suggestion is to use an IDE (NetBeans is my favorite, but Eclipse is a very common choice) and to follow Oracle lessons on the site. As for your problem : curly braces denote the start and end of a class, fields are declared inside of a class, so they must go after the first open braces.

Also : try to avoid using the static and public modifiers in fields.

Upvotes: 1

SMA
SMA

Reputation: 37033

You can't define variable outside the class, so define your scanner within your class like:

public class NewClass {
    public static Scanner input = new Scanner (System.in);
    ..
}

Static is a class variable and details about the variables are here

Upvotes: 1

Mureinik
Mureinik

Reputation: 311573

You can't just define a variable, even if it is a static variable in the middle of nowhere - it should be defined inside a class. E.g.:

import java.util.Scanner;

public class NewClass {
    // Moved inside the class
    public static Scanner input = new Scanner (System.in);

    public static void main(String args[]) {
        System.out.print("Hello");
    }

}

Upvotes: 0

Related Questions