Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:31:35+00:00 2026-06-12T11:31:35+00:00

Here is the code i am using for my classes. I have tested it

  • 0

Here is the code i am using for my classes. I have tested it many times and the functions all work correctly if i don’t serialize and deserialize.

public class Library {

    private String libTitle;
    private Vector<Album> albums;

    public String getLibTitle() {
        return libTitle;
    }

    public void setLibTitle(String libTitle) {
        this.libTitle = libTitle;
}

public Vector<Album> getAlbums() {
    return albums;
}

public void setAlbums(Vector<Album> albums) {
    this.albums = albums;
}

public Library(){
}

public Library(String libTitle) {
    this.libTitle = libTitle;
    this.albums = new Vector<Album>();
    albums.trimToSize();

}


public void addAlbum(String album){
    boolean added = false;
    for (Album alb: this.getAlbums()){
        if (alb.getAlbum()==album){
            added=true;
        }
        if (added){
            break;
        }
    }
    if (!added){
        this.getAlbums().add(new Album(album));
        this.getAlbums().trimToSize();
    }
}

public void removeAlbum(String album){
    for (Album alb : this.getAlbums()){
        if (alb.getAlbum()==album){
            this.getAlbums().remove(alb);
            this.getAlbums().trimToSize();
            break;
        }
    }
}

public void addSong(String title, String author, String album){
    boolean added = false;
    for (Album alb : this.getAlbums()){
        if (alb.getAlbum()==album){
            alb.addSong(title,author);
            added=true;
        }
        if (added){
            break;
        }
    }
    if (!added){
        this.addAlbum(album);
        this.addSong(title, author, album);
    }
}

public void removeSong(String title, String author, String album){
    boolean removed = false;
    for (Album alb : this.getAlbums()){
        if (alb.getAlbum()==album){
            alb.removeSong(title);
            if(alb.getSongs().isEmpty()){
                this.getAlbums().remove(alb);
                this.getAlbums().trimToSize();
            };
            removed=true;
        }
        if (removed){
            break;
        }
    }       
}

public void save()
{
    try {
        FileOutputStream xmlos = new FileOutputStream(this.libTitle +".xml");
            XMLEncoder encoder = new XMLEncoder(xmlos);
        encoder.writeObject(this);
        encoder.close();
        xmlos.close();
        System.out.println("Done exporting a user as xml to "+this.libTitle+".xml");
    }catch(Exception e) {
        e.printStackTrace();
    }
}

public Library restore(String lib)
{
    Library newLib = null;
    try {
        System.out.println("Importing a user as xml from "+lib+".xml");
        FileInputStream inFileStream = new FileInputStream(lib +".xml");
        XMLDecoder decoder = new XMLDecoder(inFileStream);
        newLib = (Library)decoder.readObject();
        decoder.close();
        inFileStream.close();
        System.out.println("Libloaded "+ newLib.getLibTitle());
        return newLib;

    }catch(Exception e) {
        e.printStackTrace();
    }
    return newLib;

}

}

Next is album class Which is used in the library Class

public class Album {

private String album;
private Vector<Song> songs;

public Vector<Song> getSongs() {
    return songs;
}

public void setSongs(Vector<Song> songs) {
    this.songs = songs;
}

public Album(){
}

public Album(String album) {
    this.album = album;
    this.songs = new Vector<Song>();
    songs.trimToSize();

}

public String getAlbum() {
    return album;
}

public void setAlbum(String album) {
    this.album = album;
}

public void addSong(String song, String author){
    boolean added = false;
    for (Song son : this.getSongs()){
        if (son.getTitle()==song){
            added=true;
        }
        if (added){
            break;
        }
    }
    if (!added){
        this.getSongs().add(new Song(song, author));
        this.getSongs().trimToSize();
    }
}

public void removeSong(String song){
    for (Song son : this.getSongs()){
        if (son.getTitle()==song){
            this.getSongs().remove(son);
            this.getSongs().trimToSize();
            break;
        }
    }

}

}

After that is the song class which is.

public class Song {

private String title,author;

public Song(){
}

public Song(String title, String author) {
    this.title = title;
    this.author = author;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}
}

So basically its a library i am serializing to XML and then deserializing for an app i am making. My problem is when i run the following code…..

public class Run {


public static void main(String args[]){
    Library lib = new Library("MYLibrary");
    lib.addSong("Crawling", "Lincoln Park", "Hybrid Theory");
    lib.addSong("In The End", "Lincoln Park", "Hybrid Theory");
    lib.addSong("Fire", "Pyros", "Burning Up");
    lib.addSong("Ocean", "Drowners", "Burning Up");
    lib.save();
    Library lib2 = new Library();
    lib2 =lib2.restore("MYLibrary");
    lib2.setLibTitle("test");
    lib2.removeSong("In The End", "Lincoln Park", "Hybrid Theory");
    lib2.addSong("Crawling in the dark", "me", "Hybrid Theory");
    lib2.removeSong("Crawling in the dark", "me", "Hybrid Theory");
    lib2.removeSong("Ocean", "Drowners", "Burning Up");
    lib2.removeAlbum("Hybrid Theory");
    lib2.save();

}

}

The XML file saved doesn’t have just one song like it should both XML files produced are the same. My teacher couldn’t figure out why it doesn’t work and neither can I. Why doesn’t the library get changed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:31:36+00:00Added an answer on June 12, 2026 at 11:31 am

    You are comparing album and song titles using == not .equals(). This won’t work if objects are serialized and deserialised, because the object references will have changed.

    More generally, you should compare Strings with .equals() not == in most cases. For example:

    new String("test") == "test"
    

    will evaluate to false. See this question for more explanation.

    It would be better if your Album and Song classes over-rode the .equals() and hashcode() methods, so you could compare them directly rather than having to extract their titles and compare those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the code here and I want all the functions which call SDL
I have a code here that generates XML for Engineer list using linq. My
I have a number of Eclipse projects that are all using classes from a
here is a code : using System; using Nemerle.Collections; using Nemerle.Text; //using Nemerle.Utility; using
Here is the my python code using BeautifulSoup. The main issue is with the
Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
Alright...I am here looking for some bread crumbs. This is my code: using System;
Ok, here's what I'm trying to do... given this razor code @using(Html.WriteLater()) { output
Here's the code I'm using to make a custom AlertDialog. public void onButtonClicked(View v)
Here's the code I'm using: // create a request HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.