我试图分析一个文件中的相册列表。我的列表中有22张专辑,列表中的一个项目是重复的,另一个不是正确的形式。我是应该抛出两个异常并在控制台上打印有用的消息。我的InputMismatchException工作正常,虽然我的程序应该继续读取跳过发生异常的列表中的相册对象/行的文件输入。另一个例外是DuplicateAlbumException,它是一个自定义异常。此异常应忽略重复,在控制台上打印一条有用的消息,提示已忽略副本,并且只读取一次该项。以下是我的txt文件,其中包含相册的输入:top20albums.txt
Led Zeppelin IV 1971
Led Zeppelin II 1969
Fleetwood Mac Rumors 1977
Pink Floyd The Wall 1979
XXXXX XXXXXXXXX XXXX
The Clash London Calling 1979
The Beatles Abbey Road 1969
Van Morrison Moondance 1971
Talking Heads Fear of Music 1979
Who Who's Next 1971
The Beatles Rubber Soul 1965
Cure Kiss Me, Kiss Me, Kiss Me 1987
Violent Femmes Violent Femmes 1982
Pink Floyd The Wall 1979
Soul Coughing Ruby Vroom 1994
James Laid 1993
Liz Phair Exile in Guyville 1993
Pink Floyd Dark Side of the Moon 1973
Police Zenyatta Mondatta 1980
Led Zeppelin Houses of the Holy 1973
Soul Coughing Irresistable Bliss 1996
Replacements Tim 1985
预期产出:
java AlbumList top20albums.txt
ERROR: Line 5: Invalid input for year. Skipping line
ERROR: Line 14: Duplicate album 'The Wall' by Pink Floyd
Album Rankings from top20albums.txt
Rank Title Artist Year
---- ------------------------------ -------------------- ----
1 IV Led Zeppelin 1971
2 II Led Zeppelin 1969
3 Rumors Fleetwood Mac 1977
4 The Wall Pink Floyd 1979
5 London Calling The Clash 1979
6 Abbey Road The Beatles 1969
7 Moondance Van Morrison 1971
8 Fear of Music Talking Heads 1979
9 Who's Next Who 1971
10 Rubber Soul The Beatles 1965
11 Kiss Me, Kiss Me, Kiss Me Cure 1987
12 Violent Femmes Violent Femmes 1982
13 Ruby Vroom Soul Coughing 1994
14 Laid James 1993
15 Exile in Guyville Liz Phair 1993
16 Dark Side of the Moon Pink Floyd 1973
17 Zenyatta Mondatta Police 1980
18 Houses of the Holy Led Zeppelin 1973
19 Irresistible Bliss Soul Coughing 1996
20 Tim Replacements 1985
我在eclipse控制台上的输出:
ERROR: Line 5: Invalid input for year. Skipping line.
Rank Title Artist Year
---- ----- ------ -----
1 IV Led Zeppelin 1971
2 II Led Zeppelin 1969
3 Rumors Fleetwood Mac 1977
4 The Wall Pink Floyd 1979
到目前为止,我的努力如下:
第1课
// -------------------------------------------------------------------------
/**
* This Album class has four data fields; String title,String artist,
* int year, int rank.I will be creating an ArrayList of Album object in another class later
* which I will name as AlbumList.This Album class represents each Album o object in the
* top20albums.txt file, where we have 22 albums.
*
* @author Anonymous
* @version Mar 26, 2016
*/
public class Album
{
private String title;
private String artist;
private int year;
private int rank;
public Album () {
}
public Album(String title,String artist,int year,int rank) {
//this(title,artist,year);
this.title = title;
this.artist = artist;
this.year = year;
this.rank = rank;
}
public Album(String title,String artist,int year) {
this.title=title;
this.artist=artist;
this.year=year;
rank = -1;
}
public String getTitle()
{
return title;
}
public String getArtist()
{
return artist;
}
public int getYear()
{
return year;
}
public int getRank()
{
return rank;
}
public void setRank(int rank)
{
this.rank = rank;
}
/*
* // ----------------------------------------------------------
* overridden boolean method from the Object class which tests if
* two Album objects have the same artist and title or not.Later based on the
* invokation of the method we will throw the custom DuplicateAlbumException
*/
public boolean equals(Object obj) {
if(obj instanceof Album )
return (this.title.equals(((Album)obj).title)) && (this.artist.equals(((Album)obj).artist));
else
return this == obj;
}
public String toString() {
return String.format("%-4d %-30s %-20s %-4d", rank,title,artist,year);
}
}
第2课
import java.util.*;
import java.io.*;
// -------------------------------------------------------------------------
/**
* This AlbumList class will create ArrayList of Album objects, read inputs from the
* top20Albums.txt file and then analyze the album inputs.It will print album inputs
* on the console in neat columns, throw two exceptions; one is expected to occur at
* line 5 because of the type mismatch, another is expected to occur at line 15 because of
* the album duplication.I will later create a custom DuplicateAlbumException class that
* will throw exception when the Scanner reads the duplicate file and print a helpful
* message on the console.
*
* @author Anonymous
* @version Mar 26, 2016
*/
public class AlbumList
{
private List <Album> albums ;
public AlbumList(){
albums = new ArrayList<>();
}
// ----------------------------------------------------------
/**
* This method reads file input and throws exceptions robustly.
* @param inFile
*/
public void ReadAlbumsFromFile(File inFile) {
int count = 0;
int rank = 1;
try {
Scanner input = new Scanner(inFile);
input.useDelimiter("\\t|[\\n\\r\\f]+");
while (input.hasNext()) {
String artist = input.next();
String title = input.next();
int year = input.nextInt();
Album albumObject = new Album(title,artist,year,rank);
addAlbum(albumObject);
count ++;
rank++;
}
}
// This exception will not occur unless I give it a file input that does not exist.
catch (FileNotFoundException exception)
{
System.out.println("The file was not found.");
}
/* This exception works fine.However, the Scanner should continue reading the file
* inputs just skipping the 5th line where the mismatch occur.My program for some reason
* is not printing any more input after skipping the line and that is my problem.
*/
catch (InputMismatchException exception)
{
System.out.println("ERROR: Line "+(count+1)+": Invalid input for year. Skipping line.");
}
catch (DuplicateAlbumException exception)
{
System.out.println(exception.getMessage());
Album object = new Album();
System.out.println("ERROR: Line "+(count+1)+": Duplicate album"+object.getTitle()+" by "+object.getArtist());
}
}
// ----------------------------------------------------------
/**
* This DuplicateAlbumException should work fine if the Scanner keeps reading
* even after the mismatch exception occurs, at least that is what it seems
* to me because we are still unable to read line 15 where we expect the duplicate to occur.
* The custom duplicate exception class was already made.
* @param albumObject
* @throws DuplicateAlbumException
*/
public void addAlbum (Album albumObject) throws DuplicateAlbumException {
for(int i =0; i < albums.size();i++) {
if(albums.get(i).equals(albumObject)) {
throw new DuplicateAlbumException(albumObject.getTitle(),albumObject.getArtist());
}
}
/* Look that I am adding albumObjects after the catch blocks so IT SHOULD
* continue reading file inputs even after the exception occurs but it is not doing
* so.
*/
albums.add(albumObject);
}
public void printAlbums () {
albums.toString();
for (int i = 0; i < albums.size(); i++ )
System.out.println(albums.get(i));
}
public static void main(String [] args) throws Exception {
/*This is a way to write on the console so that the user can
* write the file name on the runtime console.
*/
if (args.length != 1) {
System.out.println("java AlbumList top20albums.txt ");
System.exit(0);
}
AlbumList albumListObject = new AlbumList();
File currentFile = new File(args[0]);
albumListObject.ReadAlbumsFromFile(currentFile) ;
System.out.println("\nRank Title \t\t\t Artist \t\t Year");
System.out.println("---- ----- \t\t\t ------ \t\t -----\n");
albumListObject.printAlbums();
}
}
第3课
// -------------------------------------------------------------------------
/**
* This is my custom exception class.
*
* @author Anonymous
* @version Mar 26, 2016
*/
public class DuplicateAlbumException extends ArrayStoreException
{
public DuplicateAlbumException(String title,
String artist) {
super ("Duplicate album" + title+
"by" + artist);
}
}
我希望我的代码块有足够的信息和解释让你理解我的关注。但我会再次明确地说:为什么我的扫描仪在不匹配异常后停止读取文件输入?
答案 0 :(得分:2)
问题是.nextInt()
抛出InputMismatchException
导致控件退出while
循环。将代码重构为
try {
Scanner input = new Scanner(inFile);
input.useDelimiter("\\t|[\\n\\r\\f]+");
while (input.hasNext()) {
try{
String artist = input.next();
String title = input.next();
int year = input.nextInt();
Album albumObject = new Album(title,artist,year,rank);
addAlbum(albumObject);
count ++;
rank++;
}
catch (InputMismatchException exception)
{
System.out.println("ERROR: Line "+(count+1)+": Invalid input for year. Skipping line.");
}
}
}
// This exception will not occur unless I give it a file input that does not exist.
catch (FileNotFoundException exception)
{
System.out.println("The file was not found.");
}
/* This exception works fine.However, the Scanner should continue reading the file
* inputs just skipping the 5th line where the mismatch occur.My program for some reason
* is not printing any more input after skipping the line and that is my problem.
*/
我希望这能解决你的问题
答案 1 :(得分:0)
我们首先要使用一套,因为你不想要重复的专辑。
其次我们要说每一行都有一张专辑。这样做的原因是因为当存在输入失配异常时,扫描仪不会前进。然后你有另一个问题,你的扫描仪不在下一行。
Set<Album> albums = new HashSet<>();
try(
BufferedReader reader = Files.newBufferedReader(
inFile.toPath(), Charset.forName("UTF8")
)
){
String line;
while((line = reader.readline())!=null){
Scanner input = new Scanner(line);
input.useDelimiter("\\t|[\\n\\r\\f]+");
int rank = 0;
try{
String artist = input.next();
String title = input.next();
int year = input.nextInt();
Album albumObject = new Album(title,artist,year,rank);
if(albums.add(albumObject){
rank++;
} else{
throw new DuplicateAlbumException("album: " + title + " exists");
}
} catch(InputMissmatchException exc){
//print out an error about a bad line.
} catch(DuplicateAlbumException exc){
//print out an error about a duplicate album.
}
}
} catch(IOException e){
//problem with the file
}
还有一件事,你的Album类需要覆盖哈希码。
@Override
public int hashCode(){
return title.hashCode() + artist.hashCode();
}