1,105,225 Community Members

Arrays with if statements

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

I want to create a code that will allow me to calculate bonuses based on the 10 salarys. i need if statements for this, i need to then assign the new value to my other array i have set up called total, and then i need to display that as well.Ive tried everything with my if statements and it just wont accept anything other than doing one for each element which is no good!! I have a test class also.....any ideas? So far my code prints out the salarys in my array...

public class Salary
{


    public void calcSalary()
    {

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

    int total[];

    total=new int[11];



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

            //output each array elements value
            for (int counter=1; counter < wages.length;counter++)



            System.out.printf("%1d%11d\n",counter,wages[counter]);








    }



}

HERES MY TESTER


public class SalaryTest
{


    public static void main(String []args)
    {


    Salary pointer1=new Salary();
    pointer1.calcSalary();


    }



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

I have gotten slightly further and now have this,however it refuses to do and print my calculations. I need it to print the first array which is working fine, then do the calculations, then assign the totals of those calculations to my array called total, and then print that array too.... i dont think im very far away.?

public class Salary
{


    public void calcSalary()
    {

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

    final int TOTAL_LENGTH=11; //declare constant
    int total[]=new int[TOTAL_LENGTH];

    int counter=0;


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


        while (counter<total.length)
        {

        counter++;  


            if (wages[counter]<=19000)
            {

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


            }


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

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


            }


            if (wages[counter]>40000)
            {

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


            }






            System.out.printf("%1d%11d\n",counter,wages[counter]);  






        }




    }



 }
Member Avatar
jbennet
Moderator
17,127 posts since Apr 2005
Reputation Points: 1,618 [?]
Q&As Helped to Solve: 736 [?]
Skill Endorsements: 38 [?]
Team Colleague
Featured
 
0
 

Please use code tags.

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
 

@jbennet
Shouldn't this be moved in the java forum?

@MichaelCJ10

while (counter<total.length)
{

counter++;
...

When you start your while loop you immediately started with an increment to the counter which caused it to use the next element instead of starting with wages[0]
Next

final int TOTAL_LENGTH=11; //declare constant

The array has only 10 elements

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

ok this is what i have now, my last problem seems to be getting my whole program to work with doubles rather than ints, while my counter remains an int??? How is this going to work?

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=0;
    int counter=0;



    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
skilly
Posting Whiz in Training
270 posts since Mar 2010
Reputation Points: 23 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 0 [?]
 
0
 

sometimes it just easier to do the math than create all that code, if you can create the code, then i'm sure youy can do that math

if you really want to be a programmer, it is hard work, trial & error, there's plently of books & resources, stop crying wolf

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
 

perhaps you should download some type of IDE(integrated development environment) that will notify you of any syntax errors....

-edit sorry i get so angry, but ya know what i mean?

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

I've spent a lot of time on it, and solved most of the problems myself as i got no reply on here anyway, but this last problem of converting the ints to doubles, i just cant see how i can do that while using a counter unless i have loads of extra variables will wont be good code.

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
 

don't use int use float?

edit- yeah don't use int use float , otherwise it just superman 3 and office space problem.

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
 

my last problem seems to be getting my whole program to work with doubles rather than ints,

Initialize the variables as a double then match the arguments at the printf() statments

while my counter remains an int

if your talking about the variable counter then there's not really a need to make it a double

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