how do i declare a scanner object, filescan publicly so that i may use filescan in varios functions

i found this statement in a forum---->
private static Scanner scanner = new Scanner (1 3 4 4");

when I modified it to----->
public static Scanner filescan = new Scanner(new File("inputdata.txt"));

//declared in the main

i get the following error message
Illegal modifier for parameter scanner; only final is permitted

Thank you guys

Dani AI

Generated

Short answer for the two errors you saw: you declared the Scanner in the wrong place and you ran into a checked-exception problem when you tried to initialize it at field level.

The compiler message "Illegal modifier for parameter scanner; only final is permitted" happens because Java doesn't allow access modifiers (public/private/static) on local variables or method parameters. Declarations with those modifiers must be at class scope (outside any method). Moving the field to class scope fixes that, but then the FileNotFoundException you saw is the other issue: creating a Scanner on a File can throw a checked exception, and Java requires you either to catch it where the creation happens or to propagate it via a throws on the constructor/method.

Options that avoid the compile error (examples are different from earlier posts):

// static field + static initializer (handles exception)
public class App {
    public static java.util.Scanner fileScan;
    static {
        try {
            fileScan = new java.util.Scanner(new java.io.File("inputdata.txt"));
        } catch (java.io.FileNotFoundException e) {
            System.err.println("inputdata.txt not found: " + e.getMessage());
            fileScan = new java.util.Scanner(""); // safe fallback
        }
    }
}
// prefer this when you only use the Scanner in one method
try (java.util.Scanner sc = new java.util.Scanner(new java.io.File("inputdata.txt"))) {
    while (sc.hasNext()) {
        // process tokens
    }
} catch (java.io.FileNotFoundException e) {
    e.printStackTrace();
}

Quick best-practice checklist:

  • Prefer local scanners + try-with-resources for short-lived file reads (ensures close()).
  • Avoid public static Scanner fields—use private fields or pass a Reader/InputStream into methods for testability.
  • Don’t close a Scanner that wraps System.in unless you really want to close System.in for the whole app.
  • For long-lived static scanners, initialize inside a static block or lazily with proper exception handling, and close on shutdown if needed.

As recommended, move declaration out of the method; initialize it safely (constructor, static block, or try-with-resources) and choose the scope that matches how the Scanner is used.

Recommended Answers

All 4 Replies

Make it a class level variable - not a public static member.

Yes ..
it has to be simply

Scanner filescan = new Scanner(new File("inputdata.txt"));

FYI :

Scanner is from - java.util.Scanner

A simple text scanner which can parse primitive types and strings using regular expressions.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

For example, this code allows a user to read a number from System.in:

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

public class praogram1 
{
Scanner scanner  = new Scanner (new File ("t.txt"));
     public static void main(String[] args)
        {
        }
}

ok..im tryin to define it at the class level and i run into this issue now. How do i throw an IOException at this level

Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor

public class Program1 {

    Scanner scanner = null;
    
    public Program1(){
        try {
            scanner = new Scanner(new File("t.txt"));
        } catch (FileNotFoundException fe){
            fe.printStackTrace();
        }
    }

    public static void main(String[] args) {
        
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.