Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Array problem

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Array problem

    I realize this is an old post but my question is exactly the same. I read through this post and "Just think of mathematics" is not enough help for me.
    All I want to do is figure out how much each employee made, tabulate the count and then display the count. I'm not sure what's going on.
    All this does is cycle through the sales array and figures out the employees pay. Then counts to total[i][0]. Except it's not doing that.
    public class SalesCommissions {
    	public static void main(String[] args){
    		double[] sales = {5000, 4500, 2000, 5600, 3450, 6500, 0, 0, 3456, 9435};
    		double[][] total = {{0, 200}, {0, 300}, {0, 400}, {0, 500}, {0, 600}, {0, 700}, {0, 800}, {0, 900}, {0, 1000}};
    		double temp;		
     
    		for(int i = 0; i < sales.length-1; i++){
    			temp = 200 + (sales[i]*0.9);			
    			if(temp >= 1000){
    				total[i][0]++;
    			}else if(temp < 1000 && temp >= 900){
    				total[i][0]++;
    			}else if(temp < 900 && temp >= 800){
    				total[i][0]++;
    			}else if(temp < 800 && temp >= 700){
    				total[i][0]++;
    			}else if(temp < 700 && temp >= 600){
    				total[i][0]++;
    			}else if(temp < 600 && temp >= 500){
    				total[i][0]++;
    			}else if(temp < 500 && temp >= 400){
    				total[i][0]++;
    			}else if(temp < 400 && temp >= 300){
    				total[i][0]++;
    			}else if(temp < 300 && temp >= 200){
    				total[i][0]++;
    			}
    		}		
     
    		for(int i = 0; i < total.length; i++){
    			if(i < 8)
    				System.out.printf("$" + total[i][1] + "-" + (total[i][1]+99) + " %6.0f\n", total[i][0]);
    			if(i == 8)
    				System.out.printf("$" + total[i][1] + "+    " + " %6.0f\n", total[i][0]);
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array counting problem

    it's not doing that.
    Please explain what the problem is.
    Show the program's output and add some comments that explains what is wrong
    and show what the output should be.

    Moved to own thread.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array counting problem2

    The code compiles with no errors. It's not tabulating correctly.
    All I want to do is figure out how much each employee made, tabulate the count and then display the count. I'm not sure what's going on.
    All this does is cycle through the sales array and figures out the employees pay. Then counts to total[i][0]. <---its not doing this

    --- Update ---

    nvm, i found it. if you can find the mistake, have a good laugh

    --- Update ---

    Nvm it doesn't work.

    --- Update ---

    This does not work.
    public class SalesCommissions {
    	public static void main(String args[]){
    		double[] sales = {5000, 4500, 2000, 5600, 3450, 6500, 0, 0, 3456, 9435};
    		double[][] total = {{0, 200}, {0, 300}, {0, 400}, {0, 500}, {0, 600}, {0, 700}, {0, 800}, {0, 900}, {0, 1000}};
    		double temp;		
     
    		for(int i = 0; i < sales.length - 1; i++){
    			temp = 200 + (sales[i]*0.9);			
    			if(temp >= 1000){
    				total[i][0]++;
    			}else if(temp < 1000 && temp >= 900){
    				total[i][0]++;
    			}else if(temp < 900 && temp >= 800){
    				total[i][0]++;
    			}else if(temp < 800 && temp >= 700){
    				total[i][0]++;
    			}else if(temp < 700 && temp >= 600){
    				total[i][0]++;
    			}else if(temp < 600 && temp >= 500){
    				total[i][0]++;
    			}else if(temp < 500 && temp >= 400){
    				total[i][0]++;
    			}else if(temp < 400 && temp >= 300){
    				total[i][0]++;
    			}else if(temp < 300 && temp >= 200){
    				total[i][0]++;
    			}
    		}		
     
    		for(int i = 0; i < total.length; i++){
    			if(i < 8)
    				System.out.printf("$" + total[i][1] + "-" + (total[i][1]+99) + " %6.0f\n", total[i][0]);
    			if(i == 8)
    				System.out.printf("$" + total[i][1] + "+    " + " %6.0f\n", total[i][0]);
    		}
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array problem

    nvm, i found it.
    Have you solved it?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array problem

    No, the program compiles if you would like to view the error. The current output is.
    $200.0-299.0 1
    $300.0-399.0 1
    $400.0-499.0 1
    $500.0-599.0 1
    $600.0-699.0 1
    $700.0-799.0 1
    $800.0-899.0 1
    $900.0-999.0 1
    $1000.0+ 1
    Which is clearly incorrect...as there are three people who made 0 in sales, so there base pay + comission should be 200 and total[0][0] should equal 3 or more.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array problem

    What are supposed to be the contents of the two dimensions in the total array?
    What is in the rows
    and what is in the column(s)?

    How should the code update the contents of the array?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array problem

    The sales array holds 9 different sales of 9 employees.

    The total array holds {counter, forAmount}
    array at[0][select 0 or 1] 0 for counter 1 for amount.

    so if i say total[0][0] It's pointing to the first row, first column. However, there is some logic error, something I don't understand about arrays apparently. For starts in the first for loop its array.length-1 as the limit. Well wouldnt it just be i < array.length? i starts at 0 array.length = 9 So I want it to run 8 to but since its -1 its only going up to 7....

    total[i][0]++; should be total[i][0] = total[i][0] + 1; right?

    I'm pretty sure I set up the if else statment correctly as well, but it's not adding properly...hmmm.

    --- Update ---

    I want the first column of each row in the total array to count how many times someone made a certain amount.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array problem

    The total array holds {counter, forAmount}
    How do you find the data for an employee? If the first dim is a counter and the second is the amount?

    Is this what you mean:
    there is a row for each employee (the first dimension)
    In a row, the first column is the counter and the second column is the amount.

    For example for the second employee (index = 1)
    total[1][0] is the count for employee 1
    total[1][1] is the amount for employee 1

    When incrementing the count for an employee, the first index needs to point to that employee's row.

    I don't understand where the employee's number comes from. The if tests are for the value of temp.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array problem

    Ok, I might have explained my problem very poorly. I have been working with java for 2 years, I don't usually change the values in an array. For this instance, I am....But it won't work....Arrays in java...do weird things that regular primitives won't.

    Employees are merely identified by how much they make(no primary key or anything like that) Just how much thye made, thats their identification.
    This is held in sales[] where 9 employee sales records are.

    Now, I need to go through each record and calculate the pay = basepay + comission. basepay = 200. So, you can sell 0 dollars and still make 200 basepay.

    Now, I have have a counter for each time someone makes within a certain range.
    This is total[i][0].
    Total[i][1] is merely holding the display for each counter.

    total[0][0] = counter for 300 < temp >= 200
    total[0][1] = 200

    Also. commission is calculated by sales[i]*0.09 which means 9 percent of the sales they make are part of their pay

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array problem

    OK. There is no need to keep track of what employee the data comes from. What is desired is a count of the different amounts earned.

    Is this what you are trying to do:
    For the two dims of the array:
    first dim(the rows) is for the counts. There is one row for each amount to be counted.
    second dim (the columns) is for each count: first column is the count, the second column is the range of the amount for that count

    How does the code find the row where the count is to be incremented? Should it be using the amount for the count from the second column to find what row to increment the count in?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array problem

    It doesn't need to find it. Both for loops start at position 0 and end at array.length. Temp and the if statements determine what counter variable needs to be added too. I don't see anything wrong with this code...

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array problem

    It doesn't need to find it
    Is the "it" in that statement the "row" where the count is to be incremented?

    How will the code increment the correct count column where temp falls in the range defined by the amount column? The count and different amount values are on all the rows. How to "find" the correct row?

    I don't see anything wrong with this code...
    What is wrong with the output? Why are all the counts = 1?
    How does the value of the row index change as the code executes?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array problem

    Norm does it have anything to do with the way it's declared?
    I mean if it was a three dimensional it would be double[][][] array = {{{0,5}, {0, 6}}, etc};
    You would access the variables in the same way. array[0][0][0] = 0 array[0][0][1] = 5 array[0][1][0] = 0 array[0][1][1] = 6

    I think it has something to do with the else if statements, which seem to be accurate.... I tried using primitive types as counters and it didn't work either.
    The compiler doesn't like the first for loop unless i do array.length - 1...i think it should be array.length because im saying i < length. So if were starting at 0 we go from 0 - 8 length = 9.
    so when i = 8 8 < 9 ..so this is the last loop....but it's making me -1 to the array.length....weird and it's adding 1 to all the 0 columns

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array problem

    It has to do with "finding" the row where the counter is that should be incremented.

    Given a value of temp = 410, on what row should the counter be incremented? How can the code set the value for rowIdx in the following line of code?
    total[rowIdx][0]++;
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    Sfrius (May 31st, 2014)

  16. #15
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array problem

    Finally, I see the error of my logic, thanks for talking me through that one...
    each total[][]++ has to have the first and second bracket manually. why I put i in there..I don't know.

    --- Update ---

    for(int i = 0; i < sales.length; i++){
    			temp = 200 + (sales[i]*0.09);			
    			if(temp >= 1000){
    				total[8][0]++;
    			}else if(temp < 1000 && temp >= 900){
    				total[7][0]++;
    			}else if(temp < 900 && temp >= 800){
    				total[6][0]++;
    			}else if(temp < 800 && temp >= 700){
    				total[5][0]++;
    			}else if(temp < 700 && temp >= 600){
    				total[4][0]++;
    			}else if(temp < 600 && temp >= 500){
    				total[3][0]++;
    			}else if(temp < 500 && temp >= 400){
    				total[2][0]++;
    			}else if(temp < 400 && temp >= 300){
    				total[1][0]++;
    			}else if(temp < 300 && temp >= 200){
    				total[0][0]++;
    			}
    		}
    I also had to change .9 to 0.09....Wow....and when I fixed the i to hard coded, i < array.length worked....jeez

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array problem

    Hardcoding the row index is one way
    another way is to search the amount columns starting at the top end and working down to the first row for the row that has the amount for the value in temp.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: February 24th, 2014, 10:48 PM
  2. 2D array problem
    By namenamename in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 9th, 2013, 09:26 PM
  3. 2D array problem
    By tylin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 25th, 2013, 12:50 PM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM