Results 1 to 6 of 6
  1. #1
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default Need help with my 1st array program

    Hi again everyone,

    ive been working through my textbook and i'm just starting off with arrays, however im having a little trouble getting the output how i want it to go.

    the objective is to enter 5 intergers into an array and display the 5 intergers plus the sum of them added together in a dialog box, ive managed to do this in a terminal window however when i use dialog boxes i have a little issue with each element of the aray being displayed in a seporate box, i'm guessing ive messed up with my "for" loops or something, could someone tell me what ive done wrong or point me in the right direction please? here's what ive got so far...

    Java Code:
    import javax.swing.*;
    public class ArrDone
    {
      public static void main(String[] args)
      {
        final int NUMELS= 5;
        String s1;
        int i;
        int total = 0;
          // declare and allocate the array
        int numbers[] = new int[NUMELS]; // allocate the array
     
        for (i = 0; i < NUMELS; i++)    // Enter the grades
        {
          s1 = JOptionPane.showInputDialog("Enter a number: ");
          numbers[i] = Integer.parseInt(s1);
          total =  total + numbers[i];
          
        }
      
    
        for (i = 0; i < NUMELS; i++)     // Display and total the grades
        {
          
           
           JOptionPane.showMessageDialog(null,"numbers entered where: " + numbers[i] + "\n" + "the sum of all numbers entered is: " + total,"message", 
                                        JOptionPane.INFORMATION_MESSAGE);
           
        }
        
        System.exit(0);
      }
    }
    thanks in advance for any help, its greatly apriciated

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
        String digits = "";
        for (i = 0; i < NUMELS; i++)     // Display and total the grades
        {
          digits += numbers[i];
        }
        JOptionPane.showMessageDialog(null, "numbers entered where: " + digits +
                               "\n" + "the sum of all numbers entered is: " +
                                total, "message", 
                                JOptionPane.INFORMATION_MESSAGE);

  3. #3
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    awsome thanks for that, it helps so much having an example of how to work around these little problems,

    cheers mate/mate-ette

  4. #4
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    i have one final question reguarding arrays,

    on of the questions asks me to "write down apropriate notation for the first second and third elements of the following array int dist[15]"
    from what i have managed to read all arrays default their element values to zero unless sepcified or untill assigned, is this an incorrect assumption?

    thanks again for any help

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    all arrays default their element values to zero unless sepcified or untill assigned
    This is true for arrays of primitive data types such as int, float, double, long. Each array element is given a default value; zero for those listed.
    For objects (vis-a-vis primitives) such as Point, Rectangle, String the array elements are all null.
    Each array element is accessed by the array index which starts at zero and goes up to array.length-1.
    int[] n = new int[5];
    Each element of this array has been initialized by java with the value zero. The elements are accessed as: n[0], n[1], n[2], n[3] and n[4].
    String[] s = new String[3];
    The array is allocated. Each element is null. You have to assign a value to each element.
    For more on arrays you can try Arrays.

  6. #6
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    sweet as, thanks for clearing that up for ma hardwired, that was very concice

    ive bookmarked the array link you provided me, lots of good info condensed down in there, thanks a ton!

Similar Threads

  1. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  2. Replies: 8
    Last Post: 04-22-2008, 03:21 AM
  3. Array program help
    By adelgado0723 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •