Java compiler can't find my Scanner variable

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

?

Upvotes: 1

Views: 2786

Answers (5)

Extreme Coders
Extreme Coders

Reputation: 3511

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){ }        
}
}

Upvotes: -1

Makoto
Makoto

Reputation: 106400

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);
    }
 }

Upvotes: 4

paulsm4
paulsm4

Reputation: 121629

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());
        }        
    }
}

Upvotes: -1

Chan
Chan

Reputation: 2641

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();
        }        
    }
}

Upvotes: 0

bhuang3
bhuang3

Reputation: 3633

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

Upvotes: 0

Related Questions