Is it possible to call a constructor if we entered know value in the output screen ???

example !!

class Employee{
  int num;
  static int count=0;
  float compensation;
  public void print(){
    System.out.println ("Employee number: " + num + ", Employee Compensation: $" + compensation);
  public Employee(){
    num = 0000;
    compensation = (float)(0.0);
      public static int count() {
      return count;
class Emp{
public static void main (String args[]) throws IOException {
InputStreamReader values = new InputStreamReader (;
BufferedReader br = new BufferedReader(values);
String f,s,t,FC,SC,TC;
int F,S,T;
Employee first = new Employee();
Employee second = new Employee();
Employee third = new Employee();
float fc,sc,tc;
System.out.print("Enter Number for 1st Employee: ");
System.out.print("Enter Number for 2nd Employee: ");
System.out.print("Enter Number for 3rd Employee: ");
System.out.print("Enter Compensation of 1st Employee: ");
System.out.print("Enter Compensation of 2nd Employee: ");
System.out.print("Enter Compensation of 3rd Employee: ");
F=Integer.parseInt (f);
S=Integer.parseInt (s);
T=Integer.parseInt (t);
fc=Float.parseFloat (FC);
sc=Float.parseFloat (SC);
tc=Float.parseFloat (TC);
first.num = F;
first.compensation = fc;
second.num = S;
second.compensation = sc;
third.num = T;
third.compensation = tc;
System.out.print ("Total Employees: " + Employee.count());

When i am running a program and inputting no value for any variable then program is displaying error,

C:\Program Files\Java\jdk1.6.0_20\bin>java Emp
Enter Number for 1st Employee: 1
Enter Number for 2nd Employee: 2
Enter Number for 3rd Employee: 1
Enter Compensation of 1st Employee: 1
Enter Compensation of 2nd Employee: 1
Enter Compensation of 3rd Employee:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(
at java.lang.Float.parseFloat(
at Emp.main(

I want my program to show default value from constructor if no values entered from Keyboard.!!!
Suppose if i donot enter number and compensation of first employee then it automatically set to 0000 and 0.0

Edited 6 Years Ago by Xufyan: n/a

Use a try-catch around the parseFloat() call and only set the object value if a valid float has been entered.

I personally prefer not to mess too much with the constructor. Checking the length of your input as NormR1 mentioned or checking whether it's EMPTY is the more straight forward method.

This question has already been answered. Start a new discussion instead.