gpt4 book ai didi

java - 无法抛出我的自定义异常

转载 作者:行者123 更新时间:2023-12-02 03:46:38 24 4
gpt4 key购买 nike

我正在尝试分析文件中的专辑列表。我的列表中有 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);

}
}

我希望我的代码块有足够的信息和解释让您理解我的担忧。但我会明确再说一遍:为什么我的扫描仪在不匹配异常后停止读取文件输入?

最佳答案

问题是 .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.
*/

希望这能解决您的问题

关于java - 无法抛出我的自定义异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36241587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com