0

Would anyone know how to compare an parameter with an object? I've been trying to do it following the instructions given in the last question here: http://prntscr.com/1zaq0y But, doesn't seem to wrork.

public class Classification
    {
        String changes;
        String rating;
        String description;

    Classification()
    {
        rating = "G";
        description = "Suitable for general audiences";
    }

    Classification(String must)
    {
        if(must != "G" || must!= "PG" || must!="M")
        {
            throw new NullPointerException ("Not equal");
        }
        rating = must;
        if(rating == "G")
        {
            description = "Suitable for general audiences";
        }
        if(rating == "PG")
        {
            description = "Parental guidance recommended"; 
        }
        if(rating == "M")
        {
            description = "Suitable for mature audiences";
        }
    }

    Classification (int num, String para)
    {
        if(num!= 13 || num!= 15 || num!=16 || num!=18)
        {
            throw new IllegalArgumentException("Not equal");
        }
        if(para==null)
        {
            throw new NullPointerException("String is null");
        }
        if(num == 16)
        {
            rating = "R";
            description = para;
        }
        changes = rating + num;
    }

    void printClassification()
    {
        print("Rated " + rating + ": " + description);
    }

    void derestrict()
    {
        if(rating == "M")
        {
            rating = "PG";    
        }
        if(rating == "PG")
        {
            rating = "G";
        }
        if(rating == "G")
        {
            throw new IllegalArgumentException("G is least restrictive");
        }
        if(changes == "R18")
        {
            changes = "R16";
        }
        if(changes == "R15")
        {
            changes = "R13";
        }
    }

    Classification isLessRestrictive(Classification compare)
    {
        if(compare==null)
        {
            throw new NullPointerException ("Classification para is null");
        }
        if(changes<compare)
        {
            return true;
        }
        return false;
    }
}
Rob L
  • 1
  • 3

1 Answers1

1

When comparing Strings in Java, you need to use equals().

For example,

if(must != "G" || must!= "PG" || must!="M")

Should instead be

if(!"G".equals(must) || !"PG".equals(must) || !"M".equals(must))

And

if(rating == "G")

Would look like this:

if("G".equals(rating))
nhgrif
  • 61,578
  • 25
  • 134
  • 173