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.");
}
}
它應該閱讀的文本文件是WorldSeriesWinners.txt該文件是一個多TXT每行一個字符串。
更妙的是,'新文件( 「A2/WorldSeriesWinners.txt」)'。或者將文件從A2中移出。 – EJP
這是一個真正的@EJP –