vendredi 25 mai 2018

Code only works when using "invert if"

I edit the previous question which is inaccurate and incomplete.

The programme read 6 lines in a file, it shows below

Player,stefan@gamemail.com,SomebodyStopMe Achievement,Tutorial,6,6

Achievement,Noob missions,3,10

Achievement,Pleb missions,7,10

Achievement,Hero missions,1,10

Achievement,Boss fight,0,1

when code like this,

String line = br.readLine();
            while (line != null) 
            { 
            String[] attributes = line.split(",");
            String title = attributes[0];
            if (title.equals("Player"))
                {
                    player.setUsername(attributes[1].trim());
                    player.setTagname(attributes[2].trim());
                }else if(title.equals("Achievement"))
                {
                    description = attributes[1].trim();
                    level = Integer.parseInt(attributes[2].trim());
                    maximum = Integer.parseInt(attributes[3].trim());
                    Achievement achievement = new Achievement(description, level, maximum);
                    player.populateAchievements(achievement);
                }
            line = br.readLine();

the statement

if (title.equals("Player"))
                {
                    player.setUsername(attributes[1].trim());
                    player.setTagname(attributes[2].trim());
    }

didn't run, but when I change code to this:

if (!title.equals("Player"))
                if(title.equals("Achievement"))
                {
                    description = attributes[1].trim();
                    level = Integer.parseInt(attributes[2].trim());
                    maximum = Integer.parseInt(attributes[3].trim());
                    Achievement achievement = new Achievement(description, level, maximum);
                    player.populateAchievements(achievement);
                }else {
                    player.setUsername(attributes[1].trim());
                    player.setTagname(attributes[2].trim());
                }

it works as expected.

Two class are provided below

public class Players {

    private ArrayList<Achievement> achievements;
    private String username;
    private String tagname;

    public Players() {
        this.username = " ";
        this.tagname = " "; 
        this.achievements = new ArrayList<>();
    }

     public ArrayList<Achievement> getAchievements() {
        return achievements;
    }
    public void populateAchievements(Achievement achievement)
    {
        achievements.add(achievement);
    }
    public Achievement getAchievement(int i)
    {
        return achievements.get(i);
    }
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getTagname() {
        return tagname;
    }

    public void setTagname(String tagname) {
        this.tagname = tagname;
    }

public class Achievement {

    public Achievement(String description, int level, int maximum) {
        this.description = description;
        this.level = level;
        this.maximum = maximum;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getMaximum() {
        return maximum;
    }

    public void setMaximum(int maximum) {
        this.maximum = maximum;
    }
    private String description;
    private int level;
    private int maximum;

Aucun commentaire:

Enregistrer un commentaire