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
?
The issue is with the scope. You can declare the
Scannerobject outside of thetry...catchblock, 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
Scannerbeing instantiated inside of thetry...catchtoo, or you’ll run into issues later.Example: