Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
package persion;


public class persion {
private String name;
private String family;
private String nationalsecuritycode;


public persion()
{
}
 public persion (String name,String family,String nationalSecuritycode)
 {
     setName(name);
     setFamily(family);
     setNationalSecuritycode(nationalSecuritycode);
 }
 public String getName()
 {
     return name;
 }
 public String getFamily()
 {
     return family;
 }
  public String getNationalSecuritycode()
 {
     return nationalsecuritycode;
 }
  public void setName(String name)
  {
      if(!String.Empty(name))
      
                 return;
           this. name = name;
            
  }
  public void setFamily(String family)
  {
      if(!String.IsNullOrEmpty(family))
          return;
         this. family = family;
      
  }
  public void setNationalSecuritycode(String nationalSecuritycode)
  {
      if(nationalSecuritycode.length()!=10)
          return;
      this.nationalsecuritycode = nationalSecuritycode;
  }
}

<pre lang="cs">package persion;
public class student extends persion
{
    private String code;
    public String field;
    public String getCode()
    {
        return code;
    }
        public String getField()
    {
        return field;
    }
    public void setCode(String code)
    {
        if(code.length() != 0)
            return;
    }
     public void setField(String field)
     {
         if(String.isEmpty(field))
            return;
         String field1 = field;
     }
}


package persion;


public class Main {


    public static void main(String[] args) {

    persion persion = new persion("fatemeh","arian","0000000001");
    System.out.printf("%s ,%s ,%s",persion.getName()
    ,persion.getFamily(),persion.getNationalSecuritycode());

    student student = new student();
    student.setName("nasrin");
    student.setFamily("ami");
    student.setNationalSecuritycode("0000000002");
    }

}
Posted
Updated 22-Feb-10 2:00am
v6

You have written String.IsEmpty(str) (a static method) which does not exist. You should be using the non-static method against your object such as:
if(field == null || field.isEmpty())
    return
    ...

There are a number of other areas that need attention also; I would suggest spending some time looking at The Java Tutorials[^] would be time well spent.
 
Share this answer
 
Since you haven't actually asked a question, it is difficult to guess which of the many problems your code is showing you are complaining about.
However, given where the commented sections are, the problem is probably that the methods are doing nothing useful:
C#
public void setfamily(String family)
  {
//  if(String.isempty())
//      return;
//  this.family = family;
  String familyname = family;
  }
Taking your comments out and looking at what you have:
1)
if(String.isempty())
will not work. Firstly, because there is no static method isempty associated with strings. Secondly, even if there were, it would need to know which string it should look at. Try using
if (String.IsNullOrEmpty(family))
This will check if the string exists, and if it is empty in one go.
2) What is
String familyname = family;
going to do? The variable "familyname" is set, and immediately goes out of scope, so is thrown away.
3) This isn't an error, but is strongly recommended for all new programmers: Do not rely on the if statement followed by a single line of code. Always, but always, use curly brackets to start and end a new block. Why? Because at some point you will add a line of code above the "return" in your code, and wonder why you code suddenly fails...
4) This also isn't an error, but is also a recommended practice, particularly when you start off: Try to keep to a single exit from all methods. In other words, reverse the condition, and execute the if-block when you have something to do. It makes it clearer what is happening, that's all.
5) Again, not an error: Be consistent in your naming conventions. If you use camelCase, use it throughout. "familyname" would be "familyName". Get used to doing it early, and it becomes second nature. Wait and learn it later means you have a habit to break.
6) Still not an error: Rather than creating loads of methods "setname", "setfamily" use properties instead. It is much more natural to see myClassInstance.Name = "Joe" than to see "myClassInstance.SetName("Joe");

So, your new method would be:
C#
public string Family
   {
   get { return family; }
   set
      {
      if(!String.IsNullOrEmpty(value))
         {
         family = value;
         }
      }
   }


Sound like a lot of criticism? It isn't really; you haven't made a bad start, it's just there are things it is well worth doing properly from day 1. (And I didn't even mention commenting!)

All in all, not a bad effort - you'll get there.
 
Share this answer
 
mf_arian wrote:
but if(field == null || field.isEmpty()) doesnt work!


What do you mean "doesn't work"? If you want to earn your living in this industry you really need to make a greater effort to diagnose your problems yourself. Step one on that road is to describe exactly what you saw; for example, did you get a compile error? did the program throw an exception? did your PC crash? If you received an error message what did it say, exactly?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900