我的代码读取一个文件,其中文件名由用户键入。如果用户键入一个不存在的文件名,那么它会捕获该异常并打印未找到的文件。我试图做的是让代码循环,如果文件名无效。但是,会发生什么情况是代码一直在打印未找到的文件,并且不会停止。那么我的代码有什么问题?如何让程序检查错误,然后继续循环
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