2016-03-26 14 views
0

Ich versuche, eine Liste von Alben aus einer Datei zu analysieren.Es gibt 22 Alben in meiner Liste und eines der Elemente in der Liste ist ein Duplikat und ein anderes ist nicht in der richtigen Form.I Ich sollte zwei Ausnahmen werfen und hilfreiche Nachrichten auf der Konsole ausgeben. Meine InputMismatchException funktioniert gut, obwohl mein Programm weiterhin Dateieingaben lesen sollte und das Albumobjekt/die Zeile in der Liste überspringt, in der die Ausnahme aufgetreten ist. Eine andere Ausnahme ist eine DuplicateAlbumException ist eine benutzerdefinierte Ausnahme. Diese Ausnahme sollte das Duplikat ignorieren, eine hilfreiche Nachricht auf der Konsole ausgeben, die erwähnt, dass das Duplikat ignoriert wurde, und das Element nur einmal lesen. Das Folgende ist meine txt-Datei, die Eingänge von Alben hat: top20albums.txtKann meine benutzerdefinierte Ausnahme nicht werfen

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 

Erwartete Ausgabe:

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 

Meine Ausgabe auf der Eclipse-Konsole:

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 

Meine Bemühungen bisher ist in das Folgende:

Klasse # 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); 
} 
} 

Klasse # 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(); 


    } 
    } 

Klasse # 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); 

} 
} 

ich meine Codeblöcke haben genug Informationen und Erklärungen hoffen für Sie meine concern.But verstehen ich es nochmals ausdrücklich sagen würde : Warum hat mein Scanner die Dateieingabe nach der Mismatch-Ausnahme nicht mehr gelesen?

+1

Viel zu viel Code. Eine Frage wie diese sollte nur relevanten, minimalen Code und die tatsächlichen Fehler enthalten, um die Frage zu beantworten. Weitere Informationen dazu finden Sie in der Hilfedokumentation: http://stackoverflow.com/help/mcve – pczeus

+0

Ich bin mir nicht ganz sicher, aber soweit ich weiß, wenn ein Programm bei einem try/catch einen Fehler feststellt Anweisung es wird beendet, was auch immer es tat, also höchstwahrscheinlich hört es auf, die Datei zu lesen, wenn die Ausnahme auftritt. –

+0

@pczeus Ich wurde von einigen High-Reputation-Benutzern gesagt, dass meine Fragen nicht genug Informationen haben, deshalb habe ich mein Bestes gegeben, um zu erklären. – Afif

Antwort

2

Das Problem ist die .nextInt() wirft die InputMismatchException verursacht die Steuerung aus der while Schleife zu gehen. Refactoring den Code

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. 
*/ 

Ich hoffe, das Ihr Problem

+0

Hallo, Ihre Lösung liest nur die ersten 11 Zeilen. Es wirft immer noch nicht die doppelte Ausnahme, d. H. Es stoppt bei Zeile 5, aber nicht Zeile 6 und Zeilen nach dem nächsten Duplikat lesen. – Afif

+0

Ja, es war eine Antwort auf 'Aber ich würde es noch einmal sagen: Warum hat mein Scanner die Dateieingabe nach der Nichtübereinstimmungsausnahme gestoppt?' In den gleichen Zeilen weiterführend können Sie Ihren Ausnahmebehandler innerhalb der while-Schleife für Ausnahmen einbinden mit dem du fortfahren willst. – neur0tic

+0

Nun, du hast es dann. Ich würde wirklich weitere Hilfe zu schätzen wissen, aber danke :). – Afif

0

Zuerst löst der wir eine Reihe verwenden werden, da Sie keine doppelten Alben wollen.

Zweitens werden wir sagen, dass jede Zeile ein Album hat. Der Grund dafür ist, dass Ihr Scanner nicht weiterläuft, wenn eine Ausnahme für die Eingabefehlermeldung vorliegt. Dann hast du ein anderes Problem, wo dein Scanner nicht in der nächsten Zeile ist.

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 
} 

Noch eine Sache, Ihre Album-Klasse muss Hashcode überschreiben.

@Override 
public int hashCode(){ 
    return title.hashCode() + artist.hashCode(); 
} 
Verwandte Themen