-6
public enum MaritalStatus
{
    Single, Divorcee, Married

}

public class Person
{
    protected int ID;
    protected String FirstName;
    protected String LastName;
    protected MaritalStatus Status;
    protected int Age;

    public Person(int id,String firstname,String lastname,MaritalStatus status,int age)
    {
        ID=id;
        FirstName=firstname;
        LastName=lastname;
        Status=status;
        Age=age;
    }
    public String toString()
    {
        return System.out.println("ID: "+ID + " First Name: "+FirstName+"   Last Name: " +LastName+"    Marital Status: "+ StringStatus +"  Age: "+Age);
    }
} 

Person.java:19: error: incompatible types: MaritalStatus cannot be converted to String
        return System.out.println("ID: "+ID + " First Name: "+FirstName+"   Last Name: " +LastName+"    Marital Status: "+ (String)Status +"    Age: "+Age);
                                                                                                                               ^
1 error
Tunaki
  • 132,869
  • 46
  • 340
  • 423
saba ali
  • 3
  • 1
  • 2
  • 2
    First things first, [naming conventions](http://java.about.com/od/javasyntax/a/nameconventions.htm). Then, http://stackoverflow.com/questions/6667243/using-enum-values-as-string-literals . – Idos Jul 25 '16 at 11:34
  • 1
    So the arrow in the error message points exactly at the problem. But you didn't post this code, you posted another one that doesn't have the problem. – Tunaki Jul 25 '16 at 11:34
  • Where did `StringStatus` come from? It probably should be `status.toString()` – Murat Karagöz Jul 25 '16 at 11:35

2 Answers2

2

No there is no "error in the enum class". You have problems in the toString (see below). You probably need the following

public String toString()   {

  return  "ID: "+ID + " First Name: "+FirstName+"   Last Name: " +LastName+"    Marital Status: "+ Status +"    Age: "+Age;
}

Problems:

  • You can't return System.out.println(…) (it returns void, it does not "print-and-return-the-string")
  • To get a stringified version for status just use status in this context (you are using + on String) or status.toString() in other contexts (where String types are expected).

Other (unrelated) problems

  • fields/variables/parameter in java normally start lowercase (id, firstName etc)
  • fields are typically private and not protected
  • most people prefer spaces around the operators such as assignments (a = b)
  • (IMHO) use final when you can (like val, const in other languages`)
  • enum fields are typically in uppercase (SINGLE, MARRIED)

Here is a (more) proper version of your class:

public class Person {
    private int id;
    private String firstName;
    private String lastName;
    private MaritalStatus status;
    private int age;

    public Person(final int id, final String firstName, final String lastName, final MaritalStatus status, final int age) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.status = status;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", status=" + status +
                ", age=" + age +
                '}';
    }
}

p.s. I am guessing this is not production code, as having a field named age is a bit awkward

raphaëλ
  • 6,393
  • 2
  • 29
  • 35
0

I can see two compilation errors, plus a large pile of style errors.

This line:

return System.out.println("ID: "+ID + " First Name: "+FirstName+
   "    Last Name: " +LastName+"    Marital Status: "+ StringStatus +
   "    Age: "+Age);

1) The variable StringStatus has not been declared.

1a) You then changed that to (String) Status which is also incorrect because you can't cast a MaritalStatus to a String. (But you can call toString() on it ....)

2) The println method is a method that returns void not a String.

The primary style errors1 are:

  • Field names should NOT start with an uppercase letter.
  • Your use of whitespace before / after tokens is non-standard and inconsistent. Put a single space before and after infix operators like + and =. No spaces before a , and one space after.
  • Use of a 150+ character line is non-standard ... and makes your code hard to read. At most 80 character lines is best for maximal readability.
  • Indenting by 8 spaces is excessive.
  • Fields should be private not protected.

    This more than simply style. If you don't declare a field as private, then a subclass or (worse) some other unrelated code could potentially interfere with the field's value. That means that you have to read a lot more code to understand what might be going on when you are tracking down a bug (for example). Using private helps to encapsulate the type, which makes the code easier to read, and easier to reason about.

IMO, the best way to deal with parameter names and field names that are the same (e.g. in a constructor) is like this:

public class Person {
    protected int id;

    public Person(int id) {
        this.id = id;   // Use 'this' to disambiguate the two meanings 
                        // of the 'id' identifier.
    }
}

1 - Unfortunately, some "helpful" person has corrected a lot of this to make the code in your question readable. Please refer back to the stuff you posted originally.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216