Serkuto
Serkuto

Reputation: 45

How do I make an object which hold a string, string array, and an int array in Java?

I'm having a little trouble figuring out how to appropriately make an object which holds not only strings and ints, but also string arrays and int arrays. I currently have this

public class Gamebook {
    Section[] sections = new Section[6];
    String storyText;
    String[] choiceText;
    int[] choiceSections;

public Gamebook() { 
    storyText = "";
    choiceText = new String[1];
    choiceSections = int[];       
  }
}

If I understand correctly this will create an array of section objects, 6 of those, as well as allow for my section object to have a storyText string, a choiceText string array, and a choiceSections int array.

However, this seems hard coded, I need to be able to read through a txt file which I have, pull out the story text and other necessary information, and assign those pieces to each section object. Im finding little to no sucess and feel I may be lacking an understanding of the object orientation of Java, but after watching a couple videos on youtube most of them didn't handle objects with multiple attributes, and those that did manually set them themselves instead of using methods to parse information to fill in the "blanks".

Upvotes: 1

Views: 640

Answers (3)

SedJ601
SedJ601

Reputation: 13859

Here is an example you can play with. This uses Lists instead of Arrays. You can replace the Lists with Arrays, but you have to set a size for the Arrays in the constructor. Also, adding the Arrays will create new problems that you will have to handle.

AlbumInfo Class:

import java.util.*;

/**
 *
 * @author Sedrick
 */
public class AlbumInfo {

    private String albumName;
    private String artist;
    private List<String> tracksTitle;
    private List<String> tracksLength;

    public AlbumInfo()
    {
        albumName = "Add Album Name";
        artist = "Add Artist Name";
        tracksTitle = new ArrayList();
        tracksLength = new ArrayList();
    }

    /**
     * @return the albumName
     */
    public String getAlbumName()
    {
        return albumName;
    }

    /**
     * @param albumName the albumName to set
     */
    public void setAlbumName(String albumName)
    {
        this.albumName = albumName;
    }

    /**
     * @return the artist
     */
    public String getArtist()
    {
        return artist;
    }

    /**
     * @param artist the artist to set
     */
    public void setArtist(String artist)
    {
        this.artist = artist;
    }

    /**
     * @return the tracksTitle
     */
    public List<String> getTracksTitle()
    {
        return tracksTitle;
    }

    /**
     * @param tracksTitle the tracksTitle to set
     */
    public void addTrackTitle(String trackTitle)
    {
        this.tracksTitle.add(trackTitle);
    }

    /**
     * @return the tracksLength
     */
    public List<String> getTracksLength()
    {
        return tracksLength;
    }

    /**
     * @param tracksLength the tracksLength to set
     */
    public void addTrackLength(String trackLength)
    {
        this.tracksLength.add(trackLength);
    }

}

Main Test Class:

import java.util.*;

/**
 *
 * @author Sedrick
 */
public class AlbumTest {

    static final String[] trackTitles = {"Ambitionz Az a Ridah", "All Bout U", "Skandalouz", "Got My Mind Made Up", "How Do U Want It", "2 of Amerikaz Most Wanted", "No More Pain", "Heartz of Men", "Life Goes On", "Only God Can Judge Me", "Tradin' War Stories", "California Love(Remix)", "I Ain't Mad at Cha", "What'z Ya Phone #"};

    static final String[] trackLength = {"4:39", "4:37", "4:09", "5:14", "4:47", "4:07", "6:14", "4:43", "5:02", "4:57", "5:29", "6:25", "4:53", "5:10"};

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // Add album info
        AlbumInfo allEyesOnMeDiscOne = new AlbumInfo();
        allEyesOnMeDiscOne.setAlbumName("All Eyes On Me");
        allEyesOnMeDiscOne.setArtist("Tupac");
        for (int i = 0; i < trackTitles.length; i++) {
            allEyesOnMeDiscOne.addTrackTitle(trackTitles[i]);
        }

        for (String entry : trackLength) {
            allEyesOnMeDiscOne.addTrackLength(entry);
        }

        //Print album info
        System.out.println("Album Name: " + allEyesOnMeDiscOne.getAlbumName());
        System.out.println("Album Artist: " + allEyesOnMeDiscOne.getArtist());
        List albumTitles = allEyesOnMeDiscOne.getTracksTitle();
        List albumTitlesLength = allEyesOnMeDiscOne.getTracksLength();

        for (int i = 0; i < albumTitles.size(); i++) {
            System.out.println("Title: " + albumTitles.get(i) + "  Length: " + albumTitlesLength.get(i));
        }

    }
}

Upvotes: 1

Sophia
Sophia

Reputation: 35

Use Araylist it will store your all objects check out this tutorial it will help you or this one

Upvotes: 2

GhostCat
GhostCat

Reputation: 140427

The problem with arrays is that their size is fixed. Meaning: you need to know the number of elements when writing

someArray = new Whatever[... 

This means: when parsing information from files you have two options:

  • start with a large empty array, and keep track of the elements you put into those empty slots. You might have to copy things into a larger array when running out of space. You can copy the entries in an perfectly sized array in the end.
  • you turn to Java List and ArrayList - which allow for dynamically adding and removing of elements.

Upvotes: 1

Related Questions