2013-09-24 3 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

두 개 이상의 '스캐너'유형이 있습니다. '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!"); 
    } 

} 
관련 문제