Java 扫描仪变量不存在';不要在试抓块外工作

Java 扫描仪变量不存在';不要在试抓块外工作,java,try-catch,Java,Try Catch,我试图在try-catch块之后声明s.next(),但它不起作用!s只有在try块中才有下拉列表 我不想把解析输入混为一谈,在try块中采取适当的操作,因为它们不会抛出FNFE和IOE。我能在这里做什么 public static void main(String[] args) { // TODO Auto-generated method stub //Open file; file name specified in args (comma

我试图在try-catch块之后声明
s.next()
,但它不起作用!
s
只有在try块中才有下拉列表

我不想把解析输入混为一谈,在try块中采取适当的操作,因为它们不会抛出FNFE和IOE。我能在这里做什么

public static void main(String[] args) 
      {
        // TODO Auto-generated method stub

        //Open file; file name specified in args (command line)
        try{
            FileReader freader = new FileReader(args[0]);
            Scanner s = new Scanner(freader);

        }catch(FileNotFoundException e){
            System.err.println("Error: File not found. Exiting program...");
            e.printStackTrace();
            System.exit(-1);
        }catch(IOException e){
            System.err.println ("Error: IO exception. Exiting...");
            e.printStackTrace();
            System.exit(-1);
        }
        // if i try to declare s.next() here it would not work

我想你的意思是你想使用s.next(),但它不起作用

为此,将s声明为try/catch块外部的变量,并将其设置为null。然后将其分配到您现在分配的位置,但不带声明。如果我的假设是正确的,您的问题是s不再是try/catch之外的活动变量,因为它是在该块中声明的

FileReader freader = null;
Scanner    s       = null;
try { freader = new FileReader(args[0]); // risk null pointer exception here
      s = new Scanner(freader);
    }
catch { // etc.

我想你的意思是你想使用s.next(),但它不起作用

为此,将s声明为try/catch块外部的变量,并将其设置为null。然后将其分配到您现在分配的位置,但不带声明。如果我的假设是正确的,您的问题是s不再是try/catch之外的活动变量,因为它是在该块中声明的

FileReader freader = null;
Scanner    s       = null;
try { freader = new FileReader(args[0]); // risk null pointer exception here
      s = new Scanner(freader);
    }
catch { // etc.

因为作为
Scanner
类实例的
s
变量仅限于
try
块。如果希望
s
try-catch
块外部可访问,请在try-catch块外部声明它

 Scanner s = null;
 try{
        FileReader freader = new FileReader(args[0]);
         s = new Scanner(freader);

    }catch(FileNotFoundException e){
        System.err.println("Error: File not found. Exiting program...");
        e.printStackTrace();
        System.exit(-1);
    }catch(IOException e){
        System.err.println ("Error: IO exception. Exiting...");
        e.printStackTrace();
        System.exit(-1);
    }

因为作为
Scanner
类实例的
s
变量仅限于
try
块。如果希望
s
try-catch
块外部可访问,请在try-catch块外部声明它

 Scanner s = null;
 try{
        FileReader freader = new FileReader(args[0]);
         s = new Scanner(freader);

    }catch(FileNotFoundException e){
        System.err.println("Error: File not found. Exiting program...");
        e.printStackTrace();
        System.exit(-1);
    }catch(IOException e){
        System.err.println ("Error: IO exception. Exiting...");
        e.printStackTrace();
        System.exit(-1);
    }

在Java中,变量的作用域由声明它们的块决定。由于扫描仪是在
try
块内部构造的,因此在其外部不可见

你有什么理由想在这个区块之外进行实际的扫描操作吗?在Java 7中,一个常见的习惯用法是try with resources模式:

try (Scanner s = new Scanner(new FileInputStream(file)) {
  //Do stuff...
}

这将自动关闭扫描仪资源。实际上,您可能会泄漏它,因为在您的代码示例中没有
finally
块。

在Java中,变量的作用域由它们声明的块决定。由于扫描仪是在
try
块内部构造的,因此在其外部不可见

你有什么理由想在这个区块之外进行实际的扫描操作吗?在Java 7中,一个常见的习惯用法是try with resources模式:

try (Scanner s = new Scanner(new FileInputStream(file)) {
  //Do stuff...
}
这将自动关闭扫描仪资源。实际上,您可能会泄漏它,因为您的代码示例中没有
finally