为什么 Scanner 在读取文件时不转到下一行?

Why does Scanner not go to the next line when reading a file?

我正在使用以下代码读取文件:

Scanner fscanner = null;
try {
    fscanner = new Scanner(new File("spotify.txt"));
} catch (FileNotFoundException e) {

    e.printStackTrace();
}

while(fscanner.hasNextLine()) {
    String line = fscanner.nextLine();

    Scanner lineScanner = new Scanner(line);
    Album album = new Album();

    if(lineScanner.next().equals("CD")) {
        album.setComposers(lineScanner.next().replaceAll(",", "'s"));
        album.setAlbumName(lineScanner.useDelimiter(",").next().trim());
        album.setYear(lineScanner.next().trim());
        fscanner.nextLine();

        String c = album.getAlbumName();
        String a = album.getComposers();
        String y = album.getYear();

        System.out.println(a+" "+c+" "+y);

    }
    else if(lineScanner.next().equals("SONGS")) {
        ArrayList<String> songs = new ArrayList<String>();
        ArrayList<String> songDur = new ArrayList<String>();

        lineScanner.nextInt();

        songs.add(lineScanner.useDelimiter(",").next());
        songDur.add(lineScanner.next());

        album.setSongs(songs);
        album.setSongDurations(songDur);

        for(int i=0, j=0;i<songs.size() && j<songDur.size();i++, j++)
            System.out.println("SONG "+i+" "+songs.get(i).toString()+ " "+songDur.get(j).toString());

        System.out.println(album.showSongs());
        System.out.println(album.showSongDurations());

    }
    else if(lineScanner.next().equals("ADD")) {
        Adds adds = new Adds();

        adds.setCompanyName(lineScanner.useDelimiter(",").next().trim());
        adds.setDuration(lineScanner.next());
    }
}

文件看起来像这样:

CDS
CD U2, Songs of Innocence, 2014
SONG 1, The Miracle, 4:15
SONG 2, Every Breaking Wave, 4:12
SONG 3, California, 4:00
SONG 4, Song for Someone, 3:47
CD Coldplay, Parachutes, 2000
SONG 1, Don’t Panic, 2:17
SONG 2, Shiver, 5:00
SONG 3, Spies, 5:19
SONG 4, Sparks, 3:47
SONG 5, Yellow, 4:27
CD ImagineDragons, Night Visions, 2015
SONG 1, Demons, 3:14
SONG 2, Monster, 2:57
ADDS
ADD ING Bank, 0:20
ADD Bol.com, 0:15
ADD Albert Heijn, 0:30
ADD Specsavers, 0:15
ADD Kruidvat, 0:10
ADD MediaMarkt.nl, 0:20

问题是我在 else if(lineScanner.next().equals("SONGS")) 行得到 NoSuchElementException

我只是不知道为什么会这样,当代码实际执行并转到下一行时,如果我删除两个 else-ifs.

它给出的输出完全符合预期,但当我介绍那些 else-ifs

我该如何解决?

在第一行,您将读取第一个标记并检查它是否等于 CD ;那么如果不是,您将尝试读取另一个标记(来自同一行)以检查它是否等于 SONGS,这将在第一行引发错误,因为没有更多标记可读那条线。

您需要在 if/else if 之前将行的第一个标记读入变量,并在您的条件中引用该变量,以便它们都检查一行的第一个标记。

附带说明一下,我会避免使用两种不同的扫描仪;如果我没记错的话,你在调用 fscanner.nextLine(); 时错误地跳过了 CD 分支中的一行线)。在这种情况下,nextLine 调用应放在 if/else if 之后以避免重复。

首先检查您的 lineScanner 是否还有任何元素需要剖析。然后用一个新的变量引用lineScanner.next()的return变量这样就不用每次if-else检查的时候都调用lineScanner.next()了,因为这个方法每次都会跳转到下一个词你叫它。不过不是很明显。

Scanner fscanner = null;
    try {
        fscanner = new Scanner(new File("src/spotify.txt"));
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }

    while(fscanner.hasNextLine()) {
        String line = fscanner.nextLine();

        Scanner lineScanner = new Scanner(line);

        while(lineScanner.hasNext()){
            //Trim the word
            String word = lineScanner.next().trim();
            System.out.println(word);
        }
    }
    //Do not forget to close it
    fscanner.close();

@Arron 给了你原因。

现在也许这可能会有所帮助我相信它比扫描仪更快更好

int count = Files.lines(Paths.get(text_File_Location)).count(); //count number of lines in text file without any loop

 Path path = Paths.get(*text_File_Location, Name or URL*);

               for (int i = 0; i < count; i++) 
               {   
                    //It read exactly what we need
                    String line = Files.readAllLines(path).get(i);

                    //lets say u have data in text file in this format
                    //SONG,MUSIC,YEAR   Separated by comma


                    String everything[] = line.split(",");
                    String song = everything[0];
                    String music = everything[1];
                    String year = everything[2];

               }