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

class Read
{
    public static void main(String args[])
    {
        try {
        Scanner scan = new Scanner(new java.io.File("textfile.txt"));
        } catch (FileNotFoundException e){
        }        
        while(scan.hasNext()){
        String line = scan.nextLine();
        String[] elements = line.split(",");
        }
    }
}

Why do I get

error: cannot find symbol
        while(scan.hasNext()){
              ^
  symbol:   variable scan

?

2
  • My "Votes Cast" page shows that I don't do improper things like that. Commented May 29, 2016 at 17:07
  • Fair enough. You do know the post you just replied to is 3 1/2 years old, right? Commented May 29, 2016 at 17:15

5 Answers 5

4

The issue is with the scope. You can declare the Scanner object outside of the try...catch block, and instantiate it inside of it.

It's definitely also the case that you would want to put all of your I/O operations that depend on the Scanner being instantiated inside of the try...catch too, or you'll run into issues later.

Example:

public static void main(String[] args) {
    Scanner scan = null;
    try {
        scan = new Scanner(new File("textfile.txt"));
        // other I/O operations here including anything that uses scan
    } catch (FileNotFoundException e) {
        System.out.println("helpful error message", e);
    }
 }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I fixed it but now i get error: variable scan might not have been initialized while(scan.hasNext()){
Declare and instantiate the variable to null then. Although that scares me a bit - if you move all of your I/O operations inside of the try portion of the try...catch, you shouldn't run into that issue.
I just followed your advice and put my code in the try block and it works now (surprisingly)
0

Your scan should be declared outside the try-catch block, or you can put while loop in try-catch block

3 Comments

Be sure to set scan to null where you declare it, of you'll still get errors.
Or inside the try/catch block ... along with your while loop. It doesn't make sense to scan if you got an exception, does it? So putting it inside the loop is definitely reasonable. IMHO...
@paulsm4 Good point, thank you very much.. l learned a lot from this question though I'm not the author of it..
0

Changed the place of the where loop.

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

class Read
{
    public static void main(String args[])
    {
        try {
        Scanner scan = new Scanner(new java.io.File("textfile.txt"));
         while(scan.hasNext()){
          String line = scan.nextLine();
          String[] elements = line.split(",");
         }
        } catch (FileNotFoundException e){
           e.printStackTrace();
        }        
    }
}

1 Comment

your answer is absolutely correct. It looks like some idi0t came through and downvoted everybody. Probably didn't take his/her meds :) C'est la vie...
-1
class Read
{
    private static final String TEXT_FILE = "textfile.txt";

    public static void main(String args[])
    {
        // BAD
        try {
          Scanner scan = new Scanner(new java.io.File("textfile.txt"));
        } 
        catch (FileNotFoundException e) {
          ; // Eating exceptions - a bad habit :(
        }        
        while(scan.hasNext()){
          String line = scan.nextLine();
          String[] elements = line.split(",");
        }
    }
}

In contrast ...

class Read
{
    public static void main(String args[])
    {
        // Good
        try {
          Scanner scan = new Scanner(new java.io.File(TEXT_FILE));
          while(scan.hasNext()){
            String line = scan.nextLine();
            String[] elements = line.split(",");
          }
        } 
          catch (FileNotFoundException e){
            System.out.println ("Error parsing " + TEXT_FILE + ": " + e.getMessage());
        }        
    }
}

2 Comments

Why do you redeclare the variable? Variable shadowing definitely takes effect here.
@Duli-chan - I agree. In this case, declaring "Scanner scan" inside the try/catch block is best. I changed the code accordingly :)
-1

Try this code

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

class Read
{
public static void main(String args[])
{
    Scanner scan=null;
    try 
    {
       scan = new Scanner(new java.io.File("textfile.txt"));
       while(scan!=null)
       {
        String line = scan.nextLine();
        String[] elements = line.split(",");
       }
    } catch (FileNotFoundException e){ }        
}
}

3 Comments

If that file doesn't exist, your code blows up since you're dereferencing null.
...no you didn't. Keep at it, you'll get it soon. :)
Never play "whack-a-mole" with Makoto - he's too quick on the draw :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.