2016-06-10 3 views
1

텍스트 파일의 입력을 사용하여 문자열 배열을 만들 예정이지만 텍스트 파일을 올바르게 읽을 수 없습니다. 텍스트 파일과 프로그램이 같은 폴더에 있는데, 내가 뭘 잘못하고 있니?파일을 찾을 수 없음 예외, 원인을 잘 모르겠 음

package A2; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.PrintWriter; 
import java.util.Scanner; 

public class a2main { 
public static final Scanner SCANNER = new Scanner(System.in); 
String[] winners; 

public static void main(String[]args) throws FileNotFoundException{ //main method 
    String teamName = getInput(); 
    createArray(); 
    int wins = calcTeamWins(teamName); 
    printWins(teamName, wins); 
} 

public static int calcTeamWins(String TeamName){ //Scans the array and returns team wins 
    return 0; 
} 

public static String getInput(){ // Method to get input from user. 
    System.out.println("Please enter the name of the team you'd like to choose."); 
    return SCANNER.next(); 
} 

public static String[] createArray() throws FileNotFoundException{ //Method used to create and fill array of winners. 
    final int ARRAY_LENGTH = 104; 
    String[] winnerArray = new String[ARRAY_LENGTH]; 
    File WSW = new File("WorldSeriesWinners.txt"); 
    Scanner inputFile = new Scanner(WSW); 

    for(int i = 0; i < ARRAY_LENGTH-1; i++){ 
     while(inputFile.hasNext()){ 
     winnerArray[i] = inputFile.nextLine(); 
     System.out.println(winnerArray[i]); 
    } 
    } 
    inputFile.close(); 
    return winnerArray; 
} 
public static void printWins(String TeamName, int Wins){ //Method used to print team anem + wins 
    System.out.println("The " + TeamName + " have won " + Wins + " World Series."); 
} 

} 

이 읽을하도록되어 텍스트 파일은 파일의 각 줄에 문자열을 여러 줄 TXT입니다 WorldSeriesWinners.txt이다.

답변

0

Java가 실행되는 위치와 파일이있는 위치간에 차이가 있습니다. 클래스가있는 동일한 디렉토리에서 찾고 있지 않습니다.

패키지되었으므로 경로에 추가해야합니다.

new File(System.getProperty("user.dir") + "/A2/" + "WorldSeriesWinners.txt") 
+0

더 나은 아직,'새로운 파일 ("A2/WorldSeriesWinners.txt")': 같은

보십시오 뭔가. 또는 파일을 A2에서 위로 이동하십시오. – EJP

+0

이것은 실제 @EJP입니다. –

관련 문제