We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,737 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Conver counter from integer to double for output!!

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;






        }




    }



 }
3
Contributors
8
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
9
Views
Question
Answered
MichaelCJ10
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

@ zeroliken: thank you

skilly
Posting Whiz in Training
275 posts since Mar 2010
Reputation Points: 33
Solved Threads: 22
Skill Endorsements: 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.

MichaelCJ10
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

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

MichaelCJ10
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

MichaelCJ10
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

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!

MichaelCJ10
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by zeroliken and skilly

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0929 seconds using 2.88MB