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

How to get my array to start at element zero but call it element 1

I have created a program to display salarys, calculate a bonus payment and display total. Wages and total are my two arrays.
My problem is, when i run the program, my element starts at 0.i need it to start at one but it cant skip the first value in my array wages.

it is currently like this
0
1
2
3
4
5
6
7
8
9

i need it to display and the 10 salaries in there,it cant skip any.

1
2
3
4
5
6
7
8
9

public class Salary
{

    //main method

    public void calcSalary()
    {
        //My first array to store original salary
        //My second array is total[] to store total
        //Variables are bonus and my counter    

        double wages[]={56600,40000,30000,20320,10000,60000,35055,46000,23000,19000};

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


        //Prints out headings for my columns and my original salary and each person,0-9.


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

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

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


        //Sets my counter to zero and prints column headings

        counter=0;

        System.out.printf("\n\n\n%s\t%s\t%13s\n\n","REP","BONUS","TOTAL"); 


            //My while does the calculations and then displays the bonus in one column, and the total in another.
            //So long as my counter is less than total arrays length.

            while (counter<total.length)
            {


                //My if statements do the calculations to get the total based
                //On how much each person earns.

                if (wages[counter]<=19000)
                {


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


                }


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

                    //Less than or equal to 40 k gets a 20% Bonus

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


                }


                if (wages[counter]>40000)
                {

                    //More than 40 k Gets a 10% Bonus

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


                }




                    //After each iteration, add one to counter

                    counter=counter+1;






            }//Ends my while



    }



 }
2
Contributors
7
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
8
Views
Question
Answered
MichaelCJ10
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The array has to display 1-10 i mean rather than 0-9

MichaelCJ10
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The array has to display 1-10 i mean rather than 0-9

Add +1 to counter when printing

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

You mean before my while statement does my calculation? That would mean it will skip my first value in my array wages and ignore it won't it?

MichaelCJ10
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

no, I meant at those printf statements only

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

Nope that messes it up . It has to have something to do with my array totals length

MichaelCJ10
Newbie Poster
24 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I meant like this:

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

and next time wrap your code between

tags so I can tell exactly which line I needed to point out

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

No problem will do!

Yeah that worked perfect! So obvious no i look at it!! :(

Thanks very much i don't think i would have noticed that fair play

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

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.1649 seconds using 2.74MB