ARRAYS AGAIN????


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: ARRAYS AGAIN????

  1. #1
    ERIC Guest

    ARRAYS AGAIN????


    I have tried to do this but I can not figure out how to get this. I
    need to enter some values and put them in an array. and have those
    values put in a range example is below, below that is my code I have
    written so far. I can enter values but I can not figure out how to put
    them in the correct cells in the array so that they add up to give me
    the total in each price range. Confused yet? I am!
    Arrays are driving me nuts! Please I just need some advice to get me
    in the right direction. Thank you.






    ranges HOW MANY SALEPEOPLE IN EACH RANGE

    $0-99 ---------------- 1
    $100-199 ---------------- 2
    $200-299 ---------------- 1
    $300-399 ---------------- 3
    $400-499 ---------------- 1
    $500-599 ---------------- 0
    $600-699 ---------------- 2
    $700-799 ---------------- 0
    $800-899 ---------------- 2
    $900-999 ---------------- 0
    $1000+ ---------------- 1

    This program has got me all confused if there is anyone with
    some suggestions please feel free.


    ---------------------------------------------------------------------------
    import javax.swing.*;

    public class range
    {

    public static void main( String[] args )
    {

    int gross = 0;

    String output;


    int[] total = new int[10];



    while( gross != -1)
    {

    String input = JOptionPane.showInputDialog(null, " Enter
    The Gross Sales Amount, -1 to quit" );

    gross = Integer.parseInt( input );


    if ( gross <= 299 )
    total[0]+=total[0]++;

    if ( gross <= 399 )
    total[1]+=total[1]++;






    }


    output = "$200-$299 " + total[0] +
    "\n$300-$399 " + total[1];

    JOptionPane.showMessageDialog(null, output,"gross salaries ",

    JOptionPane.INFORMATION_MESSAGE );

    System.exit( 1 );
    }


    }

  2. #2
    wizard Guest

    Re: ARRAYS AGAIN????


    "ERIC" <wdiv@hotmail.com> wrote:
    >
    >I have tried to do this but I can not figure out how to get this. I
    >need to enter some values and put them in an array. and have those
    >values put in a range example is below, below that is my code I have
    >written so far. I can enter values but I can not figure out how to put
    >them in the correct cells in the array so that they add up to give me
    >the total in each price range. Confused yet? I am!
    >Arrays are driving me nuts! Please I just need some advice to get me
    >in the right direction. Thank you.


    > if ( gross <= 299 )
    > total[0]+=total[0]++;


    I think your problem lies in the above if statement. Your syntax is a little
    off. The operator "+=" is used as follows:
    var += value is equivalent to var = var + value
    and secondly, the increment operator "++" when used after a variable adds
    one (1) to the variable after it has been used.

    So your statement total[0]+=total[0]++ is equivalent to
    total[0] = total[0] + total[0]++
    which can return some interesting results. Let's say total[0] is 0 (zero).
    The result would be 1 in total[0] after the statement executes. However,
    if total[0] is 1, it would be 3 after another execution.

    What you probably want to use is an expression like total[0]++ and nothing
    else, that will simply increment the value.

    On another topic, does your code actually open the OptionPanes like you use
    without a main window like JFrame? I've never seen anything like this before.
    Another way to do this use JTable.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center