因此,我正在使用Scanner对象从包含公司员工(其ID,姓名和经理ID)组成的文本文件中读取数据。我遇到的问题是我存储在这些文件中的数据给出了InputMismatchException
。
这是代码:
public class DataExtracter {
private Scanner x;
private String fileName = "C:\\Users\\Jesal\\Documents\\Waqar's thing\\BT Technology Graduate Programme 2015 - Software coding exercise/atchm_3803.txt".replaceAll("[|]", "");
private File f;
private int lineCounter;
private String employeeIdLst[];
private String employeeNameLst[];
private String managerIdLst[];
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
DataExtracter bt = new DataExtracter();
de.openFile();
de.readFile();
de.closeFile();
}
public DataExtracter() {
f =new File(fileName);
lineCounter = 0;
lineCounter();
employeeIdLst = new String[lineCounter];
employeeNameLst = new String[lineCounter];
managerIdLst = new String[lineCounter];
}
public void openFile() {
try {
x = new Scanner(f);
} catch (Exception e) {
System.out.println("could not find file");
}
}
public void readFile() {
x.nextLine();
int index = 0;
while (x.hasNext() && index < lineCounter) {
String theFile = x.next().replaceAll("[|]", " ");
int employeeId = x.nextInt();
String empName = x.next();
int managerId = x.nextInt();
System.out.print(theFile);
}
index++;
}
public void closeFile() {
x.close();
}
public int lineCounter() {
openFile();
x.nextLine();
while(x.hasNext() && !x.nextLine().isEmpty()){
lineCounter++;
}
closeFile();
// System.out.println(lineCounter);
return lineCounter;
}
}
当我运行程序时没有以下代码行:
int employeeId = x.nextInt();
String empName = x.next();
int managerId = x.nextInt();
将以下内容打印到控制台:
1 Dangermouse 2 GonzotheGreat 1 3 InvisibleWoman 1 6 BlackWidow 2 12 HitGirl 3 15 SuperTed 3 16 Batman 6 17 Catwoman 6 BUILD SUCCESSFUL (total time: 0 seconds)
这是堆栈跟踪:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at binarytree.BinaryTree.readFile(BinaryTree.java:76)
at binarytree.BinaryTree.main(BinaryTree.java:41)
Java结果:1
这是文本文件的数据:
| Employee ID | Name | Manager ID |
| 1 | Dangermouse | |
| 2 | Gonzo the Great | 1 |
| 3 | Invisible Woman | 1 |
| 6 | Black Widow | 2 |
| 12 | Hit Girl | 3 |
| 15 | Super Ted | 3 |
| 16 | Batman | 6 |
| 17 | Catwoman | 6 |
我无法理解如何无法检索nextInt()和字符串,因为所检索的数据类型的顺序在整个过程中都是相同的,并且如果我正确的话,在读取输入时会丢弃空格。我还要稍后使用提取的数据将每个列数据存储在数组中或仅存储在ArrayList中。
最佳答案
它给您带来麻烦的原因是,当用户输入整数然后点击Enter时,
刚刚输入了两件事:
->>整数和\ n的“换行符”。
您正在调用的方法nextInt()仅读取整数,这会将换行符留在输入流中。但是调用nextLine()确实会读取换行符,这就是为什么在代码正常工作之前必须调用nextLine()的原因。您可能还调用了next(),该名称也已在换行符中阅读。
public class DataExtractor {
//.....
while (x.hasNext() && index < lineCounter) {
String theFile = x.next().replaceAll("[|]", " ");
int employeeId = x.nextInt();
x.nextLine(); // Added Here to read next value
String empName = x.next();
x.nextLine(); // Added Here to read next value
int managerId = x.nextInt();
System.out.print(theFile);
}
//.....
}