1

can some one please help me am having a compilation error with the last part of this code its saying create constructor please help

public class Officer {

     public static void main(String args[]) {

         Scanner input = new Scanner(System.in);

         System.out.print("Enter the name of the Soldier: ");

         String name = input.nextLine();

         System.out.print("Enter the sex of the Soldier: ");

         String sex = input.nextLine();

         System.out.print("Enter the Age of the Soldier: ");

         String age = input.nextLine();

         Soldier soldier = new Soldier(name, sex, age);
    }
}

package officer; 

public class Soldier { 

    private String soldierName; 
    private int soldierAge; 
    private char soldierSex; 

    public void Soldier( String name, char sex, int age) { 
         soldierName = name; 
         soldierSex = sex; 
         soldierAge = age; 
    } 

    public String getSoldierName() { 
        return soldierName; 
    } 
    public char getSoldierSex() { 
         return soldierSex; 
    } 
    public int getSoldierAge() { 
        return soldierAge; 
    }
} 
WonderWorld
  • 956
  • 1
  • 8
  • 18
  • 2
    do you have a constructor to match `Soldier(name,sex,age)`? – lakshman Feb 23 '14 at 13:43
  • print stackTrace here!!!!! – Devavrata Feb 23 '14 at 13:45
  • Issue might be, There is no any constructor at all in Soldier class, or different type argument in constructor like `Soldier (String name, String sex, int age)` – bNd Feb 23 '14 at 13:46
  • yes to my other class called soldier i have a constructor called public void Soldier( String name, char sex, int age) – user3343348 Feb 23 '14 at 13:49
  • here is the other class called soldier package officer; public class Soldier { private String soldierName; private int soldierAge; private char soldierSex; public void Soldier( String name, char sex, int age) { soldierName = name; soldierSex = sex; soldierAge = age; } public String getSoldierName() { return soldierName; } public char getSoldierSex() { return soldierSex; } public int getSoldierAge() { return soldierAge; }} – user3343348 Feb 23 '14 at 13:52
  • 1
    Constructors have no "return value". Look here for more: http://stackoverflow.com/questions/1788312/why-do-constructors-not-return-values – pL4Gu33 Feb 23 '14 at 13:58

3 Answers3

3

The class Soldier needs to define a matching constructor

public Soldier (String name, String sex, String age) {
    // do stuff
}

This is the method that what be executed, when you call new Soldier(name, sex, age)

Warlord
  • 2,798
  • 16
  • 21
0

It ok, except:

  • sex should be en enum type
  • age should be an integer (or float) type
  • you should validate user input

An SEX enum:

public enum SEX {
    MALE, FEMALE
}

A constructor:

public Soldier (String name, SEX sex, int age) {

}
0

You would need something like:

public class Soldier {

    public Soldier(String name, String sex, int age) {

    }
}

edit: with the new info provided you should delete the void in the public void Soldier since void would mean public void Soldier is a method of class Soldier while it's supposed to be the constructor.

WonderWorld
  • 956
  • 1
  • 8
  • 18
  • please NO single chars as parameternames ;) – pL4Gu33 Feb 23 '14 at 13:48
  • Because? I always do it like this. – WonderWorld Feb 23 '14 at 13:52
  • The names of variables should have a good name for it, that you know what the variable is and where it stands for. :) – pL4Gu33 Feb 23 '14 at 13:55
  • If i would declare `String name;` for example and then write in the constructor: `name = n; `. I don't see the problem in that. Else i would get `this.name = name;` which i find confusing. – WonderWorld Feb 23 '14 at 14:05
  • Example: If someone use your constructor and see the parameters: Soldier(String n, String s, int a), its not clear what you mean with it, he must open and read the single comments for the parameters / documentation. Topic for it: http://stackoverflow.com/questions/991757/best-practice-for-parameter-naming-in-java-constructors-and-simple-setters – pL4Gu33 Feb 23 '14 at 14:09
  • You are right. Probably `soldierName = name;` would be the way to go. More typing tho. :) – WonderWorld Feb 23 '14 at 14:16