2013-09-24 83 views
0

我是Java新手。我想通过扫描仪读取输入。 我收到错误,因为无法'实例化类型扫描器'。无法实例化类型扫描器

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

public class Factors { 

//string declaration 
static String filename; 

public static void main(String args[]){ 
    //scanner initialization, needs to be done in every program that reads from user 
    Scanner input = new Scanner(System.in); 
    int caseIndex=0; 

    //prompts user for filename 
    System.out.println("Please enter the name of the file you would like to read from."); 
    filename = input.nextString(); 

    //checks if filename exists 
    if(input.exists()) 
     System.out.println(inp.getName() + "exists!"); 
    else 
     System.out.println("File name does not exist!"); 



} 

} 

我不明白我缺乏的地方。 请帮忙。先谢谢你。

+1

请添加相关代码。否则,我们只能猜测出了什么问题。 – kiheru

+0

有多个'Scanner'类型。你确定你正在使用'java.util.Scanner'吗? –

+0

Java区分大小写。确保你有新的扫描仪(...),而不是新的扫描仪(...) – ppeterka

回答

0

Scanner没有方法nextString()exists()方法。我认为你的要求是检查该文件是否存在。这样做:

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

    public class Factors 
    { 
     static String filename; 
    public static void main(String[] args) 
    { 
      //scanner initialization, needs to be done in every program that reads from user 
      Scanner input = new Scanner(System.in); 
      int caseIndex=0; 

      //prompts user for filename 
      System.out.println("Please enter the name of the file you would like to read from."); 
      filename = input.nextLine(); 
      File file=new File(filename); 
      //checks if filename exists 
      if(file.exists()) 
       System.out.println(file.getName() + "exists!"); 
      else 
       System.out.println("File name does not exist!"); 
    } 

}