Hello guys,
I think in In java double is a primitive type which can not be null but I created a small code which requests using the JOptionPane.showInputDialog box some parameters which I send to a second class to increase a salary value (I have parse to double)
If I cancel one request, I get an error, so I want to handlded that exception without stop the progrma
THis is my code
for(empleados e: misempleados)
{
double enter = Double.parseDouble(JOptionPane.showInputDialog("en ter the %:"));
e.set_increaseSalary(enter);
}
My idea was probably use some sort of enter ==null but I found this is not possible. I want that if I cancel instead to press OK, the program still keep running. In fact, I have noticed a second issue. If I introduce a values different to numbers, I got also an error because I have parse to double.
How could I handle that?
Any idea?
I will appreciate any help
more information below:
Please, follow the complete code (this is a simple code just to test POO)
The main Class is empelado and the second Class is empelados
in the second class we have a constructor empleados which receives these values
(String nam, double salar, int year1, int month1, int day )
then, you ahve all the getters and setters that modify and return values
package poo;
import javax.swing.JOptionPane;
public class main_empleado {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
empleados empleado1 = new empleados("Paco Gomez", 85000, 1990 , 12, 17);
empleados empleado2 = new empleados("Maria Pepa", 85000, 1991 , 11, 10);
empleados empleado3 = new empleados("Loco Viejo", 85000, 1992 , 8, 12);
empleado1.set_increaseSalary(5);
empleado2.set_increaseSalary(5);
empleado3.set_increaseSalary(5);
System.out.println("Nombre :" + empleado1.getName() + " Sueldo: " + empleado1.getSalary()
+ " Fecha de Alta: " + empleado1.getDateContract());
System.out.println("Nombre :" + empleado2.getName() + " Sueldo: " + empleado2.getSalary()
+ " Fecha de Alta: " + empleado2.getDateContract());
System.out.println("Nombre :" + empleado3.getName() + " Sueldo: " + empleado3.getSalary()
+ " Fecha de Alta: " + empleado3.getDateContract());
*/
empleados[] misempleados = new empleados[4];
misempleados[0] = new empleados("Paco Gomez", 85000, 1990 , 12, 17);
misempleados[1] = new empleados("Maria Pepa", 85000, 1991 , 11, 10);
misempleados[2] = new empleados("Loco Viejo", 85000, 1992 , 8, 12);
misempleados[3] = new empleados("Nuevo empleado");
for(empleados e: misempleados)
{
double enter = Double.parseDouble(JOptionPane.showInputDialog("enter the %:"));
e.set_increaseSalary(enter);
}
for(empleados e: misempleados)
{
System.out.println("Name : " + e.getName() + " Salary: " + e.getSalary() +
" Fecha de Alta: "+ e.getDateContract() );
}
}
}
*********************
package poo;
import java.util.Date;
import java.util.GregorianCalendar;
public class empleados {
//constructor
public empleados(String nam, double salar, int year1, int month1, int day )
{
name = nam;
salary = salar;
//the month is subtracted with 1 because the calendar begin in 0, so January is 0
GregorianCalendar calendario = new GregorianCalendar(year1, month1-1, day);
altaContrato = calendario.getTime();
}
public empleados(String nam)
{
this(nam, 20000,2000,01,01);
}
//method getter return name
public String getName()
{
return name;
}
//method getter salary
public double getSalary()
{
return salary;
}
//method getter return DateContract
public Date getDateContract()
{
return altaContrato;
}
//setter increase salary
public void set_increaseSalary(double porcentage)
{
double increase = salary*porcentage/100;
salary+= increase;
}
//properties
private String name;
private double salary;
private Date altaContrato;
}