Is it possible to call a constructor if we entered know value in the output screen ???
example !!
import java.io.*;
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);
count++;
}
public static int count() {
return count;
}
}
class Emp{
public static void main (String args[]) throws IOException {
InputStreamReader values = new InputStreamReader (System.in);
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: ");
f=br.readLine();
System.out.print("Enter Number for 2nd Employee: ");
s=br.readLine();
System.out.print("Enter Number for 3rd Employee: ");
t=br.readLine();
System.out.print("Enter Compensation of 1st Employee: ");
FC=br.readLine();
System.out.print("Enter Compensation of 2nd Employee: ");
SC=br.readLine();
System.out.print("Enter Compensation of 3rd Employee: ");
TC=br.readLine();
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;
first.print();
second.print();
third.print();
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(FloatingDecimal.java:99
4)
at java.lang.Float.parseFloat(Float.java:422)
at Emp.main(Employee.java:45)
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