1,105,395 Community Members

Conver counter from integer to double for output!!

Member Avatar
MichaelCJ10
Light Poster
36 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I need everything to come out as a double rather than an int. I dont know how to do this other than creating lots of variables! Is there a way i can convert? my counter is causing the problem but i dont think theres another way to do it.The program simply prints 10 peoples wages, in an array, then it prints the bonus based on a per cent, then the total inc bonus!! and stores the new total in a new array, called total[]. .....any help would be appreciated

public class Salary
{


    public void calcSalary()
    {

    int wages[]={50000,40000,30000,20000,10000,60000,35000,46000,23000,19000};

    final int TOTAL_LENGTH=10; //declare constant
    int total[]=new int[TOTAL_LENGTH];
    int bonus;
    int counter;



    System.out.printf("\n%s\t%11s\n\n","REP","SALARY");

    for (counter=0; counter <total.length; counter++)

    System.out.printf("%2d\t%10d\n",counter, wages[counter]);


    counter=0;

    System.out.printf("\n\n\n%s\t%10s\n\n","BONUS","TOTAL"); //column headings




        while (counter<total.length)
        {




            if (wages[counter]<=19000)
            {


            bonus=wages[counter]/100*30;
            total[counter]=wages[counter]+bonus;
            System.out.printf("%d\t%10d\n",bonus,total[counter]);


            }


            if (wages[counter]>19000 && wages[counter]<=40000)
            {

            bonus=wages[counter]/100*20;
            total[counter]=wages[counter]+bonus;
            System.out.printf("%d\t%10d\n",bonus,total[counter]);


            }


            if (wages[counter]>40000)
            {

            bonus=wages[counter]/100*10;
            total[counter]=wages[counter]+bonus;
            System.out.printf("%d\t%10d\n",bonus,total[counter]);


            }







            counter=counter+1;






        }




    }



 }
Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

1. You should have posted in the Java Forum for experienced Java users to see this post
2. Why make a duplicate thread about the same problem with your last thread?
3. Are the suggestions in the other thread not helpful?

Member Avatar
skilly
Posting Whiz in Training
270 posts since Mar 2010
Reputation Points: 23 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 0 [?]
 
0
 

@ zeroliken: thank you

Member Avatar
MichaelCJ10
Light Poster
36 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i didn't get any suggestions of use, im not sure how to delete a thread , if you know you could tell me and i will.

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

well AFAIK even if you convert the values to double You can't store them in an int variable

my counter is causing the problem

which variable are you talking about?

Member Avatar
MichaelCJ10
Light Poster
36 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i need all the data in my array wages[], total[] and my bonus variable to be doubles.The problem is, i need to use my counter as an int , and i need it as it calculates the totals for me, i need a way of getting around this, rather than just creating lots of different variables. I cant calculate with an int and a double together thats my issue

Member Avatar
MichaelCJ10
Light Poster
36 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ok so i change all my variables to double. Ive done that, but my counter is still causing the issue as i cant change that to double or it wont work

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 
System.out.printf("\n%s\t%11s\n\n","REP","SALARY")
System.out.printf("\n\n\n%s\t%10\n\n","BONUS","TOTAL");

These are wrong, the arguments used here are not variables

1.Initialize the variables bonus and total array as double(no need to change variable counter as a double)
2. use %f at the printf statements for the variables which are double
3. use these for precision

bonus = (double) wages[counter]/100*30;
total[counter] = (double) wages[counter]+bonus

4. use code tags next time

Member Avatar
MichaelCJ10
Light Poster
36 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

You're right it works! i had the double in brackets before the equals as in

(double)bonus = wages[counter]/100*30;


Thank you very much It was seriously annoying me at this stage!! Im new on here so not sure about the correct way of posting and whatnot im sure i'll learn! Thanks again!

Question Answered as of 2 Years Ago by zeroliken and skilly
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: