0

While putting a .txt file into a list I keep running into a InputMismatchException error. That won't read the "MovieType" or "AlbumTitle". Relevant code has been added.

public class MovieManager {

    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<MediaItem> list = new ArrayList<>();
        Scanner inputFile = new Scanner(new File("collection.txt"));
        try {
            while (inputFile.hasNextLine()){
                String mediaType = inputFile.nextLine();
                if (mediaType.equals("Movie")){
                    String movieTitle = inputFile.nextLine();
                    //System.out.println("String" + movieTitle);
                    int movieYear = inputFile.nextInt();
                    //System.out.println("int" + movieYear);
                    String movieType = inputFile.nextLine();
                    //System.out.println("String" + movieType);
                    Movie mov = new Movie(movieTitle, movieYear, movieType);
                    list.add(mov);
                } else if (mediaType.equals("Album")) {
                    String albumArtist = inputFile.nextLine();
                    //System.out.println("String" + albumArtist);
                    int albumYear = inputFile.nextInt();
                    //System.out.println("int" + albumYear);
                    String albumTitle = inputFile.nextLine();
                    //System.out.println("String" + albumTitle);
                    Album alb = new Album(albumArtist, albumYear, albumTitle);
                    list.add(alb);
               }
            }
            inputFile.close();
            System.out.print(list);
        } catch(InputMismatchException e) {
           inputFile.next();
        }
    }
}

Collection.txt

Album
ABBA
1976
Arrival
Album
ABBA
1981 
The Visitors
Album
The Beatles
1969
Abbey Road
Album
Nazareth
1975
Hair of the Dog
Movie
Beauty and the Beast
1991
VHS
Movie
It's a Wonderful Life
1946
DVD
Movie
Tron
1983
Laserdisc
Movie
Tron: Legacy
2010
Blu-ray
AJNeufeld
  • 8,526
  • 1
  • 25
  • 44
  • You've got a lot of `println()`'s in your code - how far did the code get? – AJNeufeld Mar 31 '16 at 21:51
  • It printed everything down to the last line. But skipped the AlbumTitle and MovieType. –  Mar 31 '16 at 21:55
  • So then you must not be getting an `InputMismatchException`, are you? – AJNeufeld Mar 31 '16 at 21:59
  • What happens if `mediaType.equals("Movie")` and `mediaType.equals("Album")` both return `false`? If you can answer that, you should have a big clue as to where your problem lies. – AJNeufeld Mar 31 '16 at 22:05
  • Not since i added the try catch. The only thing outputting now is `[moviemanager.Album@3d4eac69, moviemanager.Album@42a57993, moviemanager.Album@75b84c92, moviemanager.Album@6bc7c054, moviemanager.Movie@232204a1, moviemanager.Movie@4aa298b7, moviemanager.Movie@7d4991ad, moviemanager.Movie@28d93b30]` –  Mar 31 '16 at 22:06
  • Nope! You're `try ... catch ...` would just capture the error and then exit. It is not inside the loop, so it wouldn't keep reading anything. Try again. – AJNeufeld Mar 31 '16 at 22:11
  • I took out the try catch and it did not throw any errors. And add an else to see what would happen and it is choosing that as many times its skipping `albumTitle` and `movieType` –  Mar 31 '16 at 22:19
  • I honestly have no clue what could be making it skip those lines. –  Mar 31 '16 at 22:20
  • Your input is `... ABBA \n 1976 \n Arrival \n ...`. The `movieTitle = nextLine()` consumes `ABBA \n` and return "ABBA". The `movieYear = nextInt()` call just consumes `1976`, and leaves `\n Arrival \n ...` in the input stream. Then, `movieType = nextLine()` consumes the `\n` and returns "", leaving `Arrival \n ...`. Finally, `mediaType = nextLine()` consumes the `Arrival \n`, which it didn't know what to do with, looped around, called it again, and got a real media type. – AJNeufeld Mar 31 '16 at 22:42

1 Answers1

0

With an input stream containing 1976\n, a call to Scanner#nextInt() will consume only the digit characters. It leaves the \n newline character in the input stream, for the next call to a Scanner method to deal with.

The subsequent call to Scanner#nextLine() immediately sees the \n, character, and consumes it, and returns the empty string, because after the digits 1976 to the end of the line was the empty string.

Or, visualized another way... nextLine(), nextInt(), nextLine() parses:

ABBA \n 1976 \n Arrival \n

as:

[ABBA\n][1976][\n]

returning:

"ABBA"   1976  ""

Solution:

You need to discard the remainder of the "year" line, after calling nextInt(), by immediately calling nextLine(), and ignoring the returned value.

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44