Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My Program is designed to read in album information from a .txt file, once the user inputs their desired track, the program checks an array to see if it contains that track. I have been successfully been able to read the tracks into the array, but my program cannot identify if a string lies in this array or not.

The Ruby File

require './input_functions'
module Genre
  POP, CLASSIC, JAZZ, ROCK = *1..4
end

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

class Album
    attr_accessor :title, :artist, :genre, :tracks

    def initialize (title, artist, genre)
        # insert lines here
        @title = title
        @artist = artist
        @genre = genre  
    end
end

class Track
    attr_accessor :name, :location
    def initialize (name, location)
        @name = name
        @location = location
    end
end

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


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

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

def read_album music_file
    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


def print_album album

    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


def print_track track
  puts('Track title is: ' + track.name.to_s)
    puts('Track file location is: ' + track.location.to_s)
end

** This is where I am having my problem
# search for track by name.
# Returns the index of the track or -1 if not found
def search_for_track_name(tracks, search_string)
    search_string = gets.chomp
    index = 0
while (index < tracks.length)
    tracks.include?(search_string)
    index = index + 1
end
    if tracks.include?(search_string)
        return index
    else
        index = -1

    end
    return index
end


# Reads in an Album from a file and then prints all 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()
    end

  search_string = read_string("Enter the track name you wish to find: ")
  index = search_for_track_name(album.tracks, search_string)
  if index.to_i > -1
    puts "Found " + album.tracks[index].name + " at " + index.to_s
  else
    puts "Entry not Found"
  end
    print_album(album)
end
main

This is the .txt file

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

When the user enters "Sweet Caroline", the program should return that it has found the track, instead it does:

Enter the track name you wish to find:
Sweet Caroline

Entry not Found
Album title is Greatest Hits
Album artist is Neil Diamond
Genre is 1
Pop
Track Number 0 is:
Track title is: Crackling Rose
Track file location is: sounds/01-Cracklin-rose.wav
Track Number 1 is:
Track title is: Soolaimon
Track file location is: sounds/06-Soolaimon.wav
Track Number 2 is:
Track title is: Sweet Caroline
Track file location is: sounds/20-Sweet_Caroline.wav
Tracks are [#<Track:0x000000000331be58 @name="Crackling Rose
", @location="sounds/01-Cracklin-rose.wav
">, #<Track:0x000000000331bdb8 @name="Soolaimon
", @location="sounds/06-Soolaimon.wav
">, #<Track:0x000000000331bd18 @name="Sweet Caroline
", @location="sounds/20-Sweet_Caroline.wav
">]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your problem is that album.tracks is array of Track objects

and you tried to check it like album.tracks.include?(search_string). Of course, not includes.

So your index = -1 results in "Entry not Found".

Instead of if tracks.include?(search_string) you can use

if tracks.map(&:name).include?(search_string)

but you also need to solve another problem: names in your tracks have in the end now.

So instead of track_name = music_file.gets you need

track_name = music_file.gets.chomp

Hope this will be helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...