0

I have this part of my code so far. It compiles fine and runs fine. But If I choose to insert a Rank that isn't in the list it still prints the value that I input.

private String         Rank;  //string r
private static String[] Ranks = {"Assistant", "Associate", "Full"};

public professor(String n, int a, int s, String r, int nc, int pp)
{
    super(n, a, s);
    setRank(r);
    setNumCo(nc);
    setPubPaps(pp);
}
//mutator to set the Rank and check to make sure it is in the string list
public void setRank(String r)
{
    boolean check = false;

    List valid = Arrays.asList(this.Ranks);

    if(valid.contains(r)) 
        check = true;

    if(check = true)
        this.Rank = r;
    else
        this.Rank = "Associate";
}

Here's an example of what I'm talking about:

 professor p2 = new professor ("Muench", 50, 222344455, "False", 3, 45);

"False" is definitely not in the list of ranks but it will still print as follows in my print window False professor Muench....

8
  • 1
    if(check = true) What do you think happens here? Commented Feb 25, 2016 at 10:46
  • 1
    use == instead of = where you have (check = true). You use = for assigning a value and you use == for comparisons hope that helps Commented Feb 25, 2016 at 10:48
  • if check is true then is should be one of the ranks from the array. If not I want it to just change the rank to Associate. But thanks Ronan that worked Commented Feb 25, 2016 at 10:48
  • 1
    Also, there is no reason for the variable check. Simply use the actual condition valid.contains(r) in the second if Commented Feb 25, 2016 at 10:50
  • Consider using an enum to represent the rank. The method collapses to a simple assignment (and maybe a null check). Commented Feb 25, 2016 at 10:51

4 Answers 4

1

writing it like this will work and a lot more readable

List valid = Arrays.asList(this.Ranks);

if(valid.contains(r)) 
    this.Rank = r;
else
    this.Rank = "Associate";

This works because the .contains method returns a boolean value so you basically end up with if(true) or if(false) when the value is returned

Sign up to request clarification or add additional context in comments.

1 Comment

Still suggesting to use raw-types is always bad. Either remove that part or even better fix it for him.
0
if(valid.contains(r)) 
        check = true;

    if(check == true)
        this.Rank = r;
    else
        this.Rank = "Associate";

1 Comment

Please add why this is the answer to the question. It might not be obvious from just seeing the code block.
0

You can also do it using your array of strings (without converting the array to a list):

public void setRank(String r)
{
  for ( final String rank : professor.Ranks ) // Static reference to Ranks class variable.
  {
    if ( rank.equals( r ) )
    {
      this.Rank = r;
      return;
    }
  }
  this.Rank = "Associate";
}

You also have an odd naming convention:

  • the class professor is entirely in lower case when it is usual to capitalise (or camel-case) class names.
  • the attribute Rank is capitolised when the normal convention is to use lower case.

Comments

0

Using Java 8

If case sensitive

public void setRank(String r){
       this.Rank = Stream.of(Ranks).filter(p->p.equals(r)).findAny().orElse("Associate");
}

If not case sensitive

public void setRank(String r){
       this.Rank = Stream.of(Ranks).filter(p->p.equalsIgnoreCase(r)).findAny().orElse("Associate");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.