1,105,427 Community Members

Conversion error in java

Member Avatar
Violet_82
Veteran Poster
1,002 posts since Jan 2010
Reputation Points: 14 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 8 [?]
 
0
 

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

Member Avatar
bguild
Posting Whiz
339 posts since Oct 2012
Reputation Points: 163 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 8 [?]
 
1
 

The message java.util.UnknownFormatConversionException: Conversion = 'r' means that %r is being used in a format string somewhere, and that's not allowed. It must be talking about "10% rise". Turn that into "10%% rise" and the problem will be solved. You must be careful about % in format strings.

Member Avatar
mlesniak
Junior Poster in Training
50 posts since Jul 2006
Reputation Points: 22 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
-2
 

I suggest that you change

salary += ((salary / 100) * 10);
salary += (salary * 0.10);

I suspect that although the variable, salary, was specifically assigned as a double, the type was dynamically changed as a result of the expression, because the expresssion resulted in a whole number.

Member Avatar
radhakrishna.p
Posting Whiz in Training
281 posts since Nov 2012
Reputation Points: 29 [?]
Q&As Helped to Solve: 64 [?]
Skill Endorsements: 11 [?]
 
0
 

hai Violet_82 do as bguild said in his post

it will solve your problem

i tried it and got the solution

and make this thread solved if you get your answer

happy coding

Member Avatar
Violet_82
Veteran Poster
1,002 posts since Jan 2010
Reputation Points: 14 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 8 [?]
 
1
 

thanks all for the replies. bguild, I tried what you suggested and yes problem solved. SO basically by using 10%% rise are we somehow escaping the %? Shouldn't have it been something like /%? Just so I understand exactly what's going on
thanks

Member Avatar
bguild
Posting Whiz
339 posts since Oct 2012
Reputation Points: 163 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 8 [?]
 
1
 

Much like how Java uses \ to escape characters in a String literal, format strings use %. Use \n for Java's newline, and %n for Formatter's newline. Use \\ for a backslash in a String literal, and use %% for a percentage sign in a printf format string.

You really should read http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

All of this and more is explained there. It has everything you need to know.

Member Avatar
Violet_82
Veteran Poster
1,002 posts since Jan 2010
Reputation Points: 14 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 8 [?]
 
0
 

ok thanks, quite an involved reading though, not sure I understood everything...

Member Avatar
bguild
Posting Whiz
339 posts since Oct 2012
Reputation Points: 163 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 8 [?]
 
0
 

It is intended as a reference; you only need to read the parts that are relevant for whatever you are trying to do, but you should be able to understand all of it or else it will be hard to know if you need the parts that you don't understand.

If any part of it is confusing I'm sure someone would gladly explain it when asked a specific question.

Member Avatar
jalpesh_007
Posting Whiz
385 posts since Sep 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 6 [?]
 
-1
 

you can also use String.format for printing upto any no of decimal points.
Here is the example.

System.out.println(String.format("output: %.2f", 123.456));
Output:
output: 123.46

As in your case you can modify your printing statement of increased salary by:

System.out.printf(String.format("Increased is: %.2f",employee2.salaryIncrease()));

Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
2
 

Hi jalpesh
Do you have any specific reason for suggestingSystem.out.println(String.format(... rather than System.out.printf(...
I thought that's what printf does anyway?

And what is the rationale for doing both, as in System.out.printf(String.format(" ?
J

Member Avatar
Violet_82
Veteran Poster
1,002 posts since Jan 2010
Reputation Points: 14 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 8 [?]
 
0
 

well first of all, should I include an import java.util.Formatter when I use for example String.format(...?
In this code
System.out.printf("\nSalary: \u00A3%.2f", employee2.getSalary());
If I read the documentation I can't really find this format anywhere, that's what 's confusing me
thanks

Member Avatar
bguild
Posting Whiz
339 posts since Oct 2012
Reputation Points: 163 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 8 [?]
 
0
 

import only serves two purposes. One is that it allows you to use a class without typing its package every time you type its name. So import java.util.Formatter or import java.util.* means that you can do Formatter frm = new Formatter(). Without the import, you would have to type java.util.Formatter frm = new java.util.Formatter() which is a lot more typing but accomplishes just the same thing. This is why people tend to import classes that they will use.

Imports also give people reading your code a listing of classes and packages that you will be using. This means that you shouldn't import classes that you won't use. String.format may be loosely connected to java.util.Formatter but since using String.format doesn't involve typing java.util.Formatter it wouldn't be a good idea to import java.util.Formatter unless you will actually be using it somewhere. You might consider import java.lang.String so people know you are using String, but since all of java.lang.* is automatically imported I've never seen anyone use import java.lang.String just for documentation.

The format specifier %.2f uses this syntax:

%[argument_index$][flags][width][.precision]conversion

The [] means that part of the syntax is optional, and [argument_index$][flags][width] are all optionally being skipped in %.2f. The % is the start of any format specifier, the .2 is the [.precision], and the f is the conversion which every format specifier must have.

You must have seen the list of available conversions. The listing for 'f' says "The result is formatted as a decimal number." And if you look far enough below that section you will find the section that explains precisions and it includes "For the floating-point conversions 'e', 'E', and 'f' the precision is the number of digits after the decimal separator."

So mixed in with all the other format specifier descriptions it says that %.2f means a floating point decimal number with two digits after the dot.

Member Avatar
Violet_82
Veteran Poster
1,002 posts since Jan 2010
Reputation Points: 14 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 8 [?]
 
0
 

cool, thanks for this, much clearer now!

Question Answered as of 1 Year Ago by bguild, mlesniak, JamesCherrill and 2 others
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article