Hi there, I have opened another thread even if the program is very similar.
I have modified some code I have produced earlier and now I am getting some runtime errors, and I can't quite understand why.
Now, here's the code:
EmployeeClass.java
public class EmployeeClass{
private String firstName;
private String lastName;
private double salary;
//constructor taking 3 parameters
public EmployeeClass(String name, String surname, double money){
firstName = name;
lastName = surname;
salary = money;
}
//setters to set the instance variables
public void setName(String name){
firstName = name;
}
public void setLastName(String surname){
lastName = surname;
}
public void setSalary(double money){
if(money >= 0.0){
salary = money;
}
}
//getters to get the values back
public String getName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public double getSalary(){
return salary;
}
//salary rise of 10%
public double salaryIncrease(){
salary += ((salary / 100) * 10);
return salary;
}
}
Employee.java
import java.util.Scanner;
public class Employee{
public static void main(String[] args){
EmployeeClass employee1 = new EmployeeClass("your name 1", "your surname 1", 0.0);
System.out.println("\nPrinting values '1' initialized by the constructor.");
System.out.printf("Name: %s", employee1.getName());
System.out.printf("\nSurnname: %s", employee1.getLastName());
System.out.printf("\nSalary: \u00A3%.2f", employee1.getSalary());
EmployeeClass employee2 = new EmployeeClass("your name 2", "your surname 2", 0.0);
System.out.println("\nPrinting values '2' initialized by the constructor.");
System.out.printf("Name: %s", employee2.getName());
System.out.printf("\nSurnname: %s", employee2.getLastName());
System.out.printf("\nSalary: \u00A3%.2f", employee2.getSalary());
//input data for employee1
Scanner input = new Scanner(System.in);
System.out.println("\nFirst employee's details");
System.out.println("Name: ");
String theName = input.nextLine();
System.out.println("Surnname: ");
String theSurnname = input.nextLine();
System.out.println("Salary: \u00A3");
double theSalary = input.nextDouble();
//setting the values of the instance variables of first employee
employee1.setName(theName);
employee1.setLastName(theSurnname);
employee1.setSalary(theSalary);
//getting the values of the instance variables of first employee
System.out.println("\nPrinting the values of first employee's details after having set them");
System.out.printf("Name:%s ", employee1.getName());
System.out.printf("\nSurnname: %s", employee1.getLastName());
System.out.printf("\nSalary: \u00A3%.2f", employee1.getSalary());
//increase 10% salary
System.out.printf("\na 10% rise will give a new salary of \u00A3%.2f", employee1.salaryIncrease());
//setting the values of the instance variables of second employee
System.out.println("\nSecond employee's details");
System.out.println("Name: ");
theName = input.next();
System.out.println("Surnname: ");
theSurnname = input.next();
System.out.println("Salary: \u00A3");
theSalary = input.nextDouble();
employee2.setName(theName);
employee2.setLastName(theSurnname);
employee2.setSalary(theSalary);
//getting the values of the instance variables of second employee
System.out.println("\nPrinting the values of second employee's details after having set them");
System.out.printf("Name:%s ", employee2.getName());
System.out.printf("\nSurnname:%s ", employee2.getLastName());
System.out.printf("\nSalary: \u00A3%.2f", employee2.getSalary());
//increase 10% salary
System.out.printf("\na 10% rise will give a new salary of \u00A3%.2f", employee2.salaryIncrease());
}
}
The difference here is that I am taking user input. Now when I run the program I get this:
Printing values '1' initialized by the constructor.
Name: your name 1
Surnname: your surname 1
Salary: £0.00
Printing values '2' initialized by the constructor.
Name: your name 2
Surnname: your surname 2
Salary: £0.00
First employee's details
Name:
Mic
Surnname:
Jack
Salary: £
23000
Printing the values of first employee's details after having set them
Name:Mic
Surnname: Jack
Salary: £23000.00Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'r'
at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2646)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2675)
at java.util.Formatter.parse(Formatter.java:2528)
at java.util.Formatter.format(Formatter.java:2469)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Employee.main(Employee.java:41)
antobbo@antobbo-xps17-ubuntu:~/Documents/dell xps/My documents/java/tests/account_input$
The offending lines seem to be these two:
System.out.printf("\na 10% rise will give a new salary of \u00A3%.2f", employee1.salaryIncrease(
...
System.out.printf("\na 10% rise will give a new salary of \u00A3%.2f", employee2.salaryIncrease());
because if I change them into
System.out.print("\na 10% rise will give a new salary of \u00A3" + employee1.salaryIncrease(
...
System.out.print("\na 10% rise will give a new salary of \u00A3" + employee2.salaryIncrease());
everything works fine.
It almost looks as if it thinks that salaryIncrease() isn't returning a double value..., but I am sure it is!
ANy idea?
thanks