[英]Failed to read a .csv file. Getting FileNotFoundException error after switching from JOptionPane to console I/O
Here are two versions of the same method. 这是同一方法的两个版本。 the first one uses JOptionPane and the second one uses console. 第一个使用JOptionPane,第二个使用控制台。 They are supposed to take a file path (string) from user input to find the file and read it. 他们应该从用户输入中获取文件路径(字符串)以查找文件并读取它。
The first method works just fine but the second method which uses console gave me a FileNotFoundException error. 第一种方法效果很好,但是第二种使用控制台的方法却给了我FileNotFoundException错误。 Why doesn't the second one work if they both are almost identical? 如果第二个几乎相同,为什么第二个却不起作用?
//USING JOptionPane //////////////////////////////////////////////////////////////// //使用JOptionPane ////////////////////////////////////////////// //////////////////
public void addFromFile() { 公共无效addFromFile(){
String[] option1 = { "Go back to Main Manu", "Continue" };
int choice4 = JOptionPane.showOptionDialog(null,
"Warning: this method will delete existing data before it add file data", "Monster Database",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, option1, null);
if (choice4 == 0)
monitor();
if (choice4 == 1) {
monsterAttackList.clear();
Scanner input = new Scanner(System.in);
String filePath = JOptionPane.showInputDialog(null, "Enter a filepath");
File inFile = new File(filePath);
try {
Scanner fileReader = new Scanner(inFile);
String line;
String[] part;
int attackID;
String monster;
String date;
String location;
String reporter;
while (fileReader.hasNextLine()) {
line = fileReader.nextLine();
part = line.split(",");
attackID = Integer.parseInt(part[0]);
monster = part[1];
date = part[2];
location = part[3];
reporter = part[4];
monsterAttackList.add(new MonsterAttack(attackID, monster, date, location, reporter));
}
fileReader.close(); // Close to unlock.
input.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "filepath is not valid");
System.err.println(e);
}
}
}
// USING SCANNER /////////////////////////////////////////////////////////////////////////// //使用扫描仪/////////////////////////////////////////////// /////////////////////////////
public void addFromFile() {
System.out.println("Warning: this method will delete existing data before it add file data");
System.out.println("\n1. Go back to Main Manu" + "\n2. Continue \n");
Scanner input = new Scanner(System.in);
int choice4 = input.nextInt();
if (choice4 == 1)
monitor();
if (choice4 == 2) {
monsterAttackList.clear();
System.out.println("Enter a filepath");
String filePath = input.nextLine();
File inFile = new File(filePath);
try {
Scanner fileReader = new Scanner(inFile);
String line;
String[] part;
int attackID;
String monster;
String date;
String location;
String reporter;
while (fileReader.hasNextLine()) {
line = fileReader.nextLine();
part = line.split(",");
attackID = Integer.parseInt(part[0]);
monster = part[1];
date = part[2];
location = part[3];
reporter = part[4];
monsterAttackList.add(new MonsterAttack(attackID, monster, date, location, reporter));
}
fileReader.close(); // Close to unlock.
input.close();
} catch (IOException e) {
System.out.println("filepath is not valid");
System.err.println(e);
}
}
}
You will want to put a call to input.nextLine()
after every call to input.nextInt()
. 您将想在每次调用input.nextLine()
之后调用一次input.nextInt()
。 NextInt does not consume the newline character, only the number. NextInt不使用换行符,仅使用数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.