Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6678339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:13:51+00:00 2026-05-26T04:13:51+00:00

My problem lies with trying to access Scanner scan created in one method from

  • 0

My problem lies with trying to access Scanner scan created in one method from another method. It says it cannot find variable scan. I tried declaring a global Scanner scan, but it gave me an error, non static variable name cannot be referenced from a static context. How can I access this variable?

import java.util.*;
import java.io.*;

public class MyClass {
    public static void myMethod() {
        final File f = new File("file.txt");
        Scanner scan = null;
        try {
            scan = new Scanner(f);
        }
        catch(FileNotFoundException ex) {
            System.exit(0);
        }
    }

    public static boolean anotherMethod() {
        final String s = scan.next ();

        if (s.equalsIgnoreCase ("true"))  return true;
        if (s.equalsIgnoreCase ("false")) return false;

        throw new java.util.InputMismatchException ();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:13:52+00:00Added an answer on May 26, 2026 at 4:13 am

    You’ve only declared the scan variable within the flow method. If you want to use that value within the other methods, you’ll need to take one of two courses of action:

    • Declare it as a non-local variable
    • Pass it into those other methods via a parameter

    As all your methods are static, for the first option you’d need to declare it as:

    private static Scanner scan;
    

    However, currently the flow method seems to do two radically different things:

    • It creates the Scanner (but doesn’t actually use it)
    • It tries to use data which it expects to have already been read

    It looks to me like you should be creating the Scanner in your main method (or in a new method which is called before readBoolean2D, anyway). You’re currently calling readBoolean2D as the very first action of the program, trying to read from a variable which doesn’t exist, before any Scanner has even been created.

    Note that none of this has anything to do with the try block itself – it’s the fact that the variable is declared in a separate method which is the problem (and the timing of the method calls), not the try block.

    I would try to think of restructuring the program as:

    • Open input
    • Read all the data
    • Process all the data

    I suspect you can actually get away with just local variables and parameters – the first method would return a Scanner; the second method would take a Scanner and return the data it reads; the third method would take the data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have an address field from an Access database which has been converted
Problem: Given a list of strings, find the substring which, if subtracted from the
I've got a problem trying to send a request using QNetworkAccessManager from a QObject
I have this issue with ComboBox that uses IEnumerable<Brush> as ItemsSource; the problem lies
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
I am using subsonic to access my DB in a mvc application. When trying
My problem lies with my confusion with shell variables . To my understanding, variables
I'm trying to create the following. The problem is that the stuff in row1
I'm having the following problem when trying to get the path of a given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.