Why is System.out.print() not working?

You can’t use a method while declaring the attributes/methods for a class.

public class ReadStateFile
{
    Scanner kb = new Scanner(System.in);
    String fileName;     /* everything through here compiles */
    System.out.print("Enter the file to use: "); //wrong!
}

The code should be something like this

public class ReadStateFile
{
    Scanner kb = new Scanner(System.in);
    String fileName;     /* everything through here compiles */

    public void someMethod() {
        System.out.print("Enter the file to use: "); //good!
    }
}

EDIT: based in your comment, this is what you’re trying to achieve:

public class ReadStateFile
{

    public ReadStateFile() {
        Scanner kb = new Scanner(System.in);
        String fileName;     /* everything through here compiles */
        System.out.print("Enter the file to use: ");
        //the rest of your code
    }
}

Leave a Comment