【问题标题】:Unable to extract information from a file无法从文件中提取信息
【发布时间】:2019-09-17 16:22:28
【问题描述】:

我的程序旨在从 .txt 文件中获取“专辑名称”、“专辑艺术家”、“专辑类型”和“曲目”。它将这些轨道放入一个数组中,然后将该数组打印到终端。

一切正常,除了正在打印的数组是空的“[]”

Ruby 代码

module Genre
  POP, CLASSIC, JAZZ, ROCK = *1..4
end

$genre_names = ['Null', 'Pop', 'Classic', 'Jazz', 'Rock']

class Album
# NB: you will need to add tracks to the following and the initialize()
    attr_accessor :title, :artist, :genre, :tracks

# complete the missing code:
    def initialize (title, artist, genre, tracks)
        @title = title
        @artist = artist
        @genre = genre
        @tracks = tracks
    end
end

class Track
    attr_accessor :name, :location

    def initialize (name, location)
        @name = name
        @location = location
    end
end


# Returns an array of tracks read from the given file

def read_tracks(music_file)
    tracks = Array.new
    count = music_file.gets().to_i
    index = 0
    while index < count
        track = read_track(music_file)
        tracks << track
        index = index + 1
    end

    tracks
end

# Reads in and returns a single track from the given file

def read_track(music_file)
    track_name = music_file.gets
    track_location = music_file.gets
    track = Track.new(track_name, track_location)

end




# Takes an array of tracks and prints them to the terminal

def print_tracks(tracks)
    index = 0
    while (index < tracks.length)
        puts 'Track Number ' + index.to_s + ' is:'
        print_track(tracks[index])

        index = index + 1

    end
    tracks
    end

# Reads in and returns a single album from the given file, with all its tracks

def read_album(music_file)

  # read in all the Album's fields/attributes including all the tracks
  # complete the missing code
    album_artist = music_file.gets
    album_title = music_file.gets
    album_genre = music_file.gets
    tracks = read_tracks(music_file)
    album = Album.new(album_title, album_artist, album_genre, tracks)
    album
end


# Takes a single album and prints it to the terminal along with all its tracks
def print_album(album, tracks)

  # print out all the albums fields/attributes
  # Complete the missing code.
    puts 'Album title is ' + album.title.to_s
    puts 'Album artist is ' + album.artist.to_s
    puts 'Genre is ' + album.genre.to_s
    puts $genre_names[album.genre.to_i]
    # print out the tracks
    puts 'Tracks are: ' + print_tracks(tracks).to_s
end

# Takes a single track and prints it to the terminal
def print_track(tracks)
    tracks.each do |track|
        puts('Track title is: ' + track.name.to_s)
        puts('Track file location is: ' + track.location.to_s)
    end
    end

# Reads in an album from a file and then print the album to the terminal

def main

  music_file = File.new("album.txt", "r")

if music_file
    album = read_album(music_file)
    tracks = read_tracks(music_file)
    music_file.close
else
    puts "unable to read"
end

    print_album(album, tracks)

end

main

这是 .txt 文件包含的内容

Neil Diamond
Greatest Hits
1
3
Crackling Rose
sounds/01-Cracklin-rose.wav
Soolaimon
sounds/06-Soolaimon.wav
Sweet Caroline
sounds/20-Sweet_Caroline.wav

我收到以下输出:

Album title is Greatest Hits
Album artist is Neil Diamond
Genre is 1
Pop
Tracks are: []

tracks数组何时打印出不同的track,它们的名字和文件位置

【问题讨论】:

    标签: ruby


    【解决方案1】:

    这是您的解决方案)))这是一些小错误。首先,您有空轨道,因为您读取了文件的结尾。您第一次在read_album 中被称为read_tracks,第二次在main 中被称为。 此外,当您在 print_track 中尝试在 Track 对象上调用循环时,您也会遇到异常。

    module Genre
      POP, CLASSIC, JAZZ, ROCK = *1..4
    end
    
    $genre_names = ['Null', 'Pop', 'Classic', 'Jazz', 'Rock']
    
    class Album
    # NB: you will need to add tracks to the following and the initialize()
      attr_accessor :title, :artist, :genre, :tracks
    
    # complete the missing code:
      def initialize (title, artist, genre)
        @title = title
        @artist = artist
        @genre = genre
      end
    end
    
    class Track
      attr_accessor :name, :location
    
      def initialize (name, location)
        @name = name
        @location = location
      end
    end
    
    
    # Returns an array of tracks read from the given file
    
    def read_tracks(music_file)
      tracks = Array.new
      count = music_file.gets.to_i
      index = 0
    
      while index < count
        tracks << read_track(music_file)
        index += 1
      end
    
      tracks
    end
    
    # Reads in and returns a single track from the given file
    
    def read_track(music_file)
      track_name = music_file.gets
      track_location = music_file.gets
      track = Track.new(track_name, track_location)
    end
    
    
    
    
    # Takes an array of tracks and prints them to the terminal
    
    def print_tracks(tracks)
      index = 0
      while (index < tracks.length)
        puts 'Track Number ' + index.to_s + ' is:'
        print_track(tracks[index])
    
        index += 1
    
      end
      tracks
    end
    
    # Reads in and returns a single album from the given file, with all its tracks
    
    def read_album(music_file)
    
      # read in all the Album's fields/attributes including all the tracks
      # complete the missing code
      album_artist = music_file.gets
      album_title = music_file.gets
      album_genre = music_file.gets
      album = Album.new(album_title, album_artist, album_genre)
      album
    end
    
    
    # Takes a single album and prints it to the terminal along with all its tracks
    def print_album(album)
    
      # print out all the albums fields/attributes
      # Complete the missing code.
      puts 'Album title is ' + album.title.to_s
      puts 'Album artist is ' + album.artist.to_s
      puts 'Genre is ' + album.genre.to_s
      puts $genre_names[album.genre.to_i]
      # print out the tracks
      puts 'Tracks are: ' + print_tracks(album.tracks).to_s
    end
    
    # Takes a single track and prints it to the terminal
    def print_track(track)
      puts('Track title is: ' + track.name.to_s)
      puts('Track file location is: ' + track.location.to_s)
    end
    
    # Reads in an album from a file and then print the album to the terminal
    
    def main
    
      music_file = File.new("album.txt", "r")
    
      if music_file
        album = read_album(music_file)
        album.tracks = read_tracks(music_file)
        music_file.close
      else
        puts "unable to read"
      end
    
      print_album(album)
    
    end
    
    main
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多