I am new to java and trying out a question but I have no idea why it can't calculate my commission

        public static void main(String[] args) {
           Scanner keyboard= new Scanner(System.in);// input form keyboard

            double retail_price;
            double emp_num;
            char trans_code;
            double a =0.06; 
            double b = 0.08;       
            double commission;




         System.out.println("Please enter the retail price");
         retail_price=keyboard.nextDouble();   
         System.out.println("Please enter your transcation code");
         trans_code=keyboard.next().charAt(0);

         if (trans_code== a){
            commission= (retail_price*a);
         }
            else
             if (trans_code== b){
             commission= (retail_price*b);
             }




              System.out.println("your commission " +commission);



        }
    }

`

I'm assuming that the entered trans_code is to be either 'a' or 'b'. The problem is that you are then comparing transcode (which is either the character 'a' or 'b') against variables a and b, which equal 0.06 and 0.08 respectively. The comparison fails and you don't get the result you expect.
You want:
If (trans_code == 'a')

I'm assuming that the entered trans_code is to be either 'a' or 'b'.

I agree with hericles in his input assumptions and his solution. I will add that it is a good idea to display to the screen what the legal options are, like so:

System.out.println("Please enter a single character for your transcation code: either 'a' or 'b' : ");

That takes the guesswork out of things. Also, what if the person enters something other than 'a' or 'b'? I see an "if" handling 'a' and an "else if" for handling 'b', but I see no "else" statement for handling bad data. I noticed that Java gave me a warning that commision may not have been initialized. Sure enough, when I looked at the if-block, I noticed there was no "else". It's a good idea to have an "else" statement that tells the user he/she entered invalid data and either ends the program or gives them an option to try again.

Its not complete yet and i am just trying out, but everytime i input a,b or c the program get error. The qnestion I am trying out is "A transaction record on a sales commission file contains the retail price of an

item sold, a transaction code that indicates the sales commission category to

which an item can belong, and the employee number of the person who sold the

item. The transaction code can contain the values A, B or C, which indicate that

the percentage commission will be 6%, 8% or 10% respectively. Construct a

JAVA program using netbean that will read a record on the file, calculate the commission owing for

that record, and print the retail price, commission and employee number."

Maybe I should read and study more before trying out again :(

Share that error. Otherwise you are asking others to debug. I see folk will read a few lines but let's here what line blew up and then backtrack from there.

Click Here
Click Here
here is the error, I tried diff way and let it auto correct my commission and a,b,c to =0 or what so it works but it does not calculate correctly.

You had a good answer 22 hours ago.

I must be a idiot, I still don't understand and can't correct what is wrong T_T

Okay I manage to get it working, does it look okay?
Click Here

Okay I manage to get it working, does it look okay?

You tell us. You say you've managed to "get it working", so I assume you've thrown everything you can at it and it spits out the right results.

Click Here

It's preferable to copy and paste the code itself. We can't see the entire program in the photo and we can't copy and paste the program into NetBeans and compile it ourselves. That said, a few comments...

From your output: "your commission 1.0". The math may be right, but it looks better you display "Your commission is $1.00." Proper capitalization, grammar, and an ending period is easy, so you might as well do it. Having it display "$1.00" instead of "1.0" is a little trickier and that may or may not be worth the extra effort to you. A few links for you.

http://docs.oracle.com/javase/tutorial/i18n/format/numberintro.html
http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

Don't let it scare you too bad. There are a lot of words in the above links, but here's a quick and dirty single line. Change line 30 in your original post to this:

System.out.println("Your commission is " + NumberFormat.getCurrencyInstance(new Locale("en", "US")).format(commission) + ".");

You'll need to import java.text.NumberFormat and java.util.Locale. The nice thing is you are using NetyBeans, so click that little yellow light bulb/red exclamation point that NetBeans throws up next to the offending code and NetBeans gives you a few choices to fix the import problem in a couple of clicks.

A couple comments on the variables a, b, and c. Those are not very descriptive names and they don't vary, so how about making them constants (do this in Java by using the "final" keyword) and calling them COMMISSION_RATE_A, COMMISSION_RATE_B, and COMMISSION_RATE_C. Have a variable called commissionRate and assign that value to either COMMISSION_RATE_A, COMMISSION_RATE_B, or COMMISSION_RATE_C in your if-block. Then have a line AFTER the if-block that calculates the commission with the line commission = retail_price * commissionRate;

It's not a huge deal, but in my opinion, it makes the code easier to read and figure out. I don't need to know anything about your assignment to be able to follow it. The calculation dovetails immediately with my life experience, plus the variable names add to that. When I see something multiplied by a variable called a, I need to go up top to see what a is declared as and see if it means anything. When you name things the way I did here, it's like I'm reading an English sentence, not code.

All in all, it looks good. Throw a bunch of different input at it, make sure it all works, then either turn it in as-is or if you want, incorporate the minor changes I mentioned above. One final note though. Make sure that 'A', 'B', and 'C' are not considered legal input. If they are, you'll need to handle them. But the nice thing is that you've explicitly told the user to enter it in as lower-case, so people won't keep entering upper-case and wonder why it all failed. Still though, if you are supposed to handle the input in either lower- or upper-case, you'll need to add that.

Thank you so much for your help I will keep all your comment in mind and work harder. cheers!

Hi assert null,

so how do we change that particular question to switch ?

How do we create this question in Switch Case, reading the ID and retail price from FileNotFoundException ;

A transaction record on a sales commission file contains the retail price of an item sold, a transaction code that indicates the sales commission category to which an item can belong, and the employee number of the person who sold the item. The transaction code can contain the values A, B or C, which indicate that the percentage commission will be 6%, 8% or 10% respectively. Construct an algorithm that will read a record on the file, calculate the commission owing for that record, and print the retail price, commission and employee number.

Any if-else if-else code, where the criteria is comparing an integral type (i.e. byte, short, char, int, etc.) to particular values can be transformed into a switch statement. For example, in this case where trans_code is a character and the legal values are 'a', 'b', and 'c', you might have the following if-else if-else code...

if(trans_code == 'a')
{
    // code to execute when trans_code is 'a'
}
else if(trans_code == 'b')
{
    // code to execute when trans_code is 'b'
}
else if(trans_code == 'c')
{
    // code to execute when trans_code is 'c'
}
else
{
    // code to execute when trans_code is none of the above
}

The equivalent switch code would be...

switch(trans_code)
{
    case 'a': // code to execute when trans_code is 'a'
        break;
    case 'b': // code to execute when trans_code is 'b'
        break;
    case 'c': // code to execute when trans_code is 'c'
        break;
    default: // code to execute when trans_code is none of the above
        break;
}

As for the FileNotFoundException, the program could not find the input file either because the file doesn't exist, the program is looking in the wrong directory, the file is misnamed, the file is locked by some other process, or there is some permissions issue.

just for the record... you can also switch on String values and enums. In this case a String would seem more natural for a transaction code, even if it is only 1 char today.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.