0

I just began studying how to code 2 weeks ago and I seem to be having trouble with the idea of using CharSequence.

I am writing simple program, based on the tv show Game of Thrones, that lets me print out names, their allegiances and whether or not they are alive or deceased using an ArrayList.

This is my characters class:

import java.util.ArrayList;

public class Characters {

/* Fields */
private String name;
private String allegiance;
private String status;
private int seasonSeen;


/* Constructor */
public Characters(String name, String allegiance, String status, int seasonSeen) {
    this.name = name;
    this.allegiance = allegiance;
    this.status = status;
    this.seasonSeen = seasonSeen;
}

/* Methods */

public String getName() {
    return name;
}

public String getallegiance() {
    return allegiance;
}

public String getStatus() {
    return status;
}

public int getseasonSeen() {
    return seasonSeen;
}

}

This is my test class

import java.util.ArrayList;

public class TestGoTCharacters {

public static void main(String[] args) {


// create an arraylist called character
ArrayList<Characters> character = new ArrayList<Characters>();

    // character paramaters(name, allegiance, status, season appearance) into the arraylist
    character.add(new Characters("Jon Snow", "House Stark, The Nights Watch", "Alive", 1));
    character.add(new Characters("Neddard Stark", "House Stark", "Deceased", 1));
    character.add(new Characters("Arya Stark", "House Stark, Faceless Men", "Alive", 1));
    character.add(new Characters("Catelyn Stark", "House Stark", "Deceased", 1));
    character.add(new Characters("Sansa Stark", "House Stark", "Alive", 1));
    character.add(new Characters("Bran Stark", "House Stark", "Alive", 1));
    character.add(new Characters("Rickon Stark", "House Stark", "Deceased", 1));
    character.add(new Characters("Cersei Lannister","House Lannister, House Baratheon", "Alive", 1));
    character.add(new Characters("Jamie Lannister", "House Lannister, Kingsguard", "Alive", 1));
    character.add(new Characters("Tyrion Lannister", "House Lannister, House Targaryen", "Alive", 1));
    character.add(new Characters("Tywin Lannister", "House Lannister", "Deceased", 1));
    character.add(new Characters("Robert Baratheon", "House Baratheon, House Baratheon of Kings Landing", "Deceased", 1));
    character.add(new Characters("Stannis Baratheon", "House Baratheon, House Baratheon of Dragonstone", "Deceased", 2));
    character.add(new Characters("Renly Baratheon", "House Baratheon", "Deceased", 1 ));
    character.add(new Characters("Melissandre", "The lord of light", "Alive", 2));
    character.add(new Characters("Margaery Tyrell", "House Tyrell, House Baratheon, House Baratheon of Kings Landing", "Deceased", 2));
    character.add(new Characters("Sandor 'The Hound' Clegane", "House Baratheon of Kings Landing, Brotherhood without Banners", "Alive", 1 ));
    character.add(new Characters("Joffrey Baratheon", "House Baratheon of Kings Landing, House Lannister", "Deceased", 1));
    character.add(new Characters("Gregor 'The Mountain' Clegane", "House Lannister", "Alive", 1));
    character.add(new Characters("Khal Drogo", "Dothraki", "Deceased", 1));
    character.add(new Characters("Benjen Stark", "House Stark, The Nights Watch", "Alive", 1));
    character.add(new Characters("Ramsay Bolton", "House Bolton", "Deceased", 3));
    character.add(new Characters("Daenarys Targayen", "House Targaryen", "Alive", 1));
    character.add(new Characters("Viserys Targaryen", "House Targaryen", "Deceased", 1));
    character.add(new Characters("Rhaegar Targaryen", "House Targaryen", "Deceased", 7));
    character.add(new Characters("Petyr Baelish", "House Baelish", "Alive", 1));
    character.add(new Characters("Robb Stark", "House Stark", "Deceased", 1));
    character.add(new Characters("Theon Greyjoy", "House Targaryen, House Stark, House Greyjoy", "Alive", 1));
    character.add(new Characters("Yara Greyjoy", "House Greyjoy", "Alive", 2));



    /* method that reads out names in the array list */
    /*
    int count = 1;
    System.out.println("Some of the characters in Game of Thrones, are: ");
    for (int i = 0; i < character.size(); i++) {
        System.out.println(count + ". " + character.get(i).getName()); 
        count = count + 1;
    }
    */


    /* method that finds house allegiances */

    System.out.println("\n"); // create a line space
    System.out.println("The following characters are loyal to House Stark: ");
    CharSequence HouseStark = "House Stark";  // if Allegiance contains House Stark then, print the name
    for (int i = 0; i < character.size(); i++) {
        // if (characer.get(i_.getallegiance() == "House Stark") 
        if (character.get(i).getallegiance() == HouseStark)// compares if allegiance paramters is true (equals house stark)
            System.out.println(character.get(i).getName());
    }



    /* method that finds deceased */

    /* method that finds characters seen in season 1 */

    /* method that finds characters seen other than season 1 */

}
}

This is what it reads, but I seem to notice characters like "Jon Snow" isn't in the output even though I mentioned in the parameters "House Stark, The Nights Watch"

The following characters are loyal to House Stark: 
Neddard Stark
Catelyn Stark
Sansa Stark
Bran Stark
Rickon Stark
Robb Stark
iVince905
  • 1
  • 1
  • 1
  • 1
    I wonder what does this `new Characters("Jon Snow", "House Stark, The Nights Watch", "Alive", 1)` resolve into? When there is only `/* Constructor */ public Characters(String name, String allegiance, String status, int seasonSeen)` – Naman Jan 25 '17 at 04:59
  • Also why do you even use `CharSequence` ?http://stackoverflow.com/questions/1049228/charsequence-vs-string-in-java – Naman Jan 25 '17 at 05:04

1 Answers1

0

You have a comma in "Jon Snow"'s allegiance (with more than one value, so they are not equal). allegiance is a String, so you could use String.contains like

if (character.get(i).getallegiance().contains(HouseStark))
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249