我的代码读取用户输入文件名的文件。如果用户键入不存在的文件名,则会捕获异常并打印未找到的文件。我试图做的是如果文件名无效,则循环代码。然而,会发生的是代码继续打印出未找到的文件,它不会停止。那我的代码出了什么问题?
public static Scanner readFile(String filename){
File input = new File(filename);
Scanner sc = null;
do {
try {
sc = new Scanner(input);
}
catch(FileNotFoundException e) {
System.out.println("Filename not valid");
}
} while (!new File(filename).exists());
return sc;
}
没有一个答案对我有帮助,所以也许我会尝试发布整个代码,看看是否有帮助。
import java.io.*;
import java.util.*;
public class Report{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String filename = scanner.next();
Scanner input = readFile(filename);
CO2Data[] aDataArray = null;
aDataArray = readData(filename);
String highestvalue = highest(aDataArray);
String lowestvalue = lowest(aDataArray);
String highest_road = highest_per_person(aDataArray);
String lowest_road = lowest_per_person(aDataArray);
try{
PrintStream output = new PrintStream(new File("Report.txt"));
output.println("The country with the lowest CO2 emissions is " + lowestvalue);
output.println("The country with the highest CO2 emissions is " + highestvalue);
output.println();
output.println("The country with the lowest per person road emissions is " + lowest_road);
output.println("The country with the highest per person road emissions is " + highest_road);
}
catch(FileNotFoundException e){
System.out.println("Error printing to file");
System.exit(-1);
}
}
public static Scanner readFile(String filename){
Scanner stdin = new Scanner(System.in);
File input;
do {
input = new File(filename);
try {
stdin = new Scanner(input);
}
catch(FileNotFoundException e) {
System.out.println("Filename not valid. Please try again:");
filename = stdin.nextLine();
}
} while (!input.exists());
return stdin;
}
public static CO2Data[] readData(String filename){
File input = new File(filename);
Scanner sc = null;
try{
sc = new Scanner(input);
}
catch(FileNotFoundException e){
System.out.println("Filename not valid");
System.exit(-1);
}
String info = sc.nextLine();
int total = sc.nextInt();
CO2Data[] arr = new CO2Data[total];
for(int i=0; i<10;i++){
arr[i] = new CO2Data();
}
for(int i=0; i<10;i++){
arr[i].setCountry(sc.next());
arr[i].setTotalCO2(sc.nextDouble());
arr[i].setRoadCO2(sc.nextDouble());
arr[i].setCO2PerPerson(sc.nextDouble());
arr[i].setCarsPerPerson(sc.nextInt());
}
return arr;
}
public static String highest (CO2Data [] arr2){
Scanner sc = new Scanner(System.in);
CO2Data highestindex = arr2[0];
for (int i = 0; i<arr2.length; i++){
if (arr2[i].getTotalCO2() > highestindex.getTotalCO2()){
highestindex = arr2[i];
}
}
return highestindex.getCountry();
}
public static String lowest (CO2Data [] arr3){
Scanner sc = new Scanner(System.in);
CO2Data lowestindex = arr3[0];
for (int i = 0; i<arr3.length; i++){
if (arr3[i].getTotalCO2() < lowestindex.getTotalCO2()){
lowestindex = arr3[i];
}
}
return lowestindex.getCountry();
}
public static String highest_per_person (CO2Data [] arr2){
Scanner sc = new Scanner(System.in);
CO2Data highestindex = arr2[0];
for (int i = 0; i<arr2.length; i++){
if (arr2[i].getRoadCO2() > highestindex.getRoadCO2()){
highestindex = arr2[i];
}
}
return highestindex.getCountry();
}
public static String lowest_per_person (CO2Data [] arr3){
Scanner sc = new Scanner(System.in);
CO2Data lowestindex = arr3[0];
for (int i = 0; i<arr3.length; i++){
if (arr3[i].getRoadCO2() < lowestindex.getRoadCO2()){
lowestindex = arr3[i];
}
}
return lowestindex.getCountry();
}
}
答案 0 :(得分:0)
当文件名无效时,您应该从用户那里获得一个新的文件名,而不是一遍又一遍地重新检查相同的文件名。 E.g:
Scanner stdin = new Scanner(System.in);
File input = new File(filename);
Scanner sc = null;
do {
try {
sc = new Scanner(input);
}
catch(FileNotFoundException e) {
System.out.println("Filename not valid. Please try again:");
filename = stdin.readLine();
}
} while (!new File(filename).exists());
答案 1 :(得分:0)
readFile(String filename)
收到filename
参数,但如果此类文件不存在,该方法可以做什么?它肯定无法自己发明正确的文件名。
所以问题不在于这种方法。您需要确保使用有效的文件名调用此方法。
public Scanner getScanner() {
Scanner stdin = new Scanner(System.in);
while (true) {
String path = null;
try {
System.out.println("Please enter path to input file: ");
path = stdin.nextLine();
return getScanner(path);
} catch (FileNotFoundException e) {
System.err.println("No such file: " + path);
}
}
}
private Scanner getScanner(String path) throws FileNotFoundException {
return new Scanner(new File(path));
}