从我创建的包含字符串和数字的文本文件中扫描数据时,我收到输入不匹配异常。
我放了一个try-catch语句来尝试更好地理解错误,只是忽略它。
我的司机班:
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
public class BookShelf {
public static void main(String[] args) throws IOException{
Scanner scan = new Scanner(new File("./src/Books.txt"));
ArrayList<Books> books = new ArrayList<Books>();
String author, title, publisher;
int copyright;
int count =0;
try {
while(scan.hasNext()){
title = scan.nextLine();
author = scan.nextLine();
publisher = scan.nextLine();
copyright = scan.nextInt();
Books book = new Books(title,author,publisher,copyright);
books.add(book);
}
} catch (Exception e){
System.out.println(e);
}
while (count <books.size() ){
System.out.println(books.get(count));
count++;
}
}
}
My Books构造函数类:
public class Books {
private String title, author, publisher;
private int copyright;
public Books(String newTitle, String newAuthor, String newPub, int newCopy ){
title = newTitle;
author = newAuthor;
publisher = newPub;
copyright = newCopy;
}
// Setters
public void setTitle(String newTitle){
title = newTitle;
}
public void setAuthor(String newAuthor){
author = newAuthor;
}
public void setPublisher(String newPub){
publisher = newPub;
}
public void setCopyrightDate(int newCopy){
copyright = newCopy;
}
// Getters
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public String getPublisher(){
return publisher;
}
public int getCopyrightDate(){
return copyright;
}
public String toString(){
String result ="";
result ="Title: "+title +"\nAuthor: "+author+"\nPublisher: "+ publisher;
result += "\nCopyright Date: "+copyright;
return result;
}
}
我的文字文件:
Java Software Solutions
Lewis & Loftus
Addison-Wesley
2012
Faces in Time
Lewis Aleman
Megalodon Entertainment
2012
Purple Cow
Seth Godin
Portfolio
2002
根据我的文本文件的顺序和我的while循环中的数据类型,我无法理解为什么我的代码输入不匹配。