我的代碼讀取一個文件,其中文件名由用戶鍵入。如果用戶鍵入一個不存在的文件名,那麼它會捕獲該異常並打印未找到的文件。我試圖做的是讓代碼循環,如果文件名無效。但是,會發生什麼情況是代碼一直在打印未找到的文件,並且不會停止。那麼我的代碼有什麼問題?如何讓程序檢查錯誤,然後繼續循環
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();
}
}
你不是要求在循環中有更多的輸入,並且你永遠不會爲傳遞給函數的參數指定'filename'。 – khelwood 2014-12-03 20:34:52
你所有的循環正在做的是重複檢查相同的文件名一遍又一遍。 – 2014-12-03 20:35:07
我知道,但問題是我不知道如何使代碼檢查另一個文件。 – 2014-12-03 20:37:35