Results 1 to 12 of 12
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default JOptionPane looping?

    Java Code:
    int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int sum = 0;
    // use for-each style for to display and sum the values
    for(int x : nums) {
    JOptionPane.showMessageDialog(null,"Value is: " + x);
    sum += x;
    
    }
    here's my problem
    Java Code:
    JOptionPane.showMessageDialog(null,"Value is: " + x);
    it keeps saying the message "Value is" and initializing the JOptionPane everytime its looping into the array opposite to the output that i expected.

    the output i want is to put all the value in one optionpane.

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: JOptionPane looping?

    If you want every value use the Arrays.toString like this JOptionPane.showMessageDialog(null, Arrays.toString(nums));
    And of course place it outside the loop because now, every time a new pane is created.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: JOptionPane looping?

    JOptionPane is smart enough to deal with an array as the message parameter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: JOptionPane looping?

    Yes, but wouldn't it just return the memory location in hex?

  5. #5
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: JOptionPane looping?

    In your scenario above, if you are trying to get 55 as the result, then you need to move the showMessageDialog outside of the loop as it is evaluating each time and change the variable you are asking to see to show the sum instead of x.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: JOptionPane looping?

    Quote Originally Posted by superhaNds View Post
    Yes, but wouldn't it just return the memory location in hex?
    Ah right. That trick doesn't work for primitive arrays.

    Try this:
    Java Code:
    JOptionPane.showMessageDialog(null, new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
    Oh, and that's not the 'memory location in hex' it's the value returned by the array's toString().

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: JOptionPane looping?

    Of course. But it just returns the class name with "@" and the hashcode in hexadecimal, which is based on the memory address of the instance

  8. #8
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: JOptionPane looping?

    Thanks now i know how to put that in One Pane and thanks for the alternative method, now java is getting more interesting than i thought :D

  9. #9
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: JOptionPane looping?

    I have a question to ask i modify the code
    Java Code:
      public static void main(String...args){
    int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int sum = 0;
    int x;
    
    for(x= 0;x<nums.length+1;x++) {
    sum += x;
    
    }
    JOptionPane.showMessageDialog(null,"Value is: "+nums[x]+ "\nSummation: "+sum);
                                              }
    \
    instead of using a foreach loop i used the normal for loop but i have a problem
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    	at assistprog.ArProg.main(ArProg.java:33)
    Java Result: 1
    i having that error everytime i put the JOptionPane outside the loop but when put inside the loop is doesn't show any error but it keeps initializing the pane.
    the methods that you suggest is exactly what i need but i want to know if there's a way to figure this out and gives the right output.
    Last edited by raffs03; 06-16-2013 at 06:42 AM.

  10. #10
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: JOptionPane looping?

    You aren't asking the output to show you the total value.... Is that what you are trying to get? 55?
    This line should probably be different:
    Java Code:
    JOptionPane.showMessageDialog(null,"Value is: "+nums[x]);
    Should be:
    Java Code:
    JOptionPane.showMessageDialog(null,"Value is: "+ sum);
    Does that work?

  11. #11
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: JOptionPane looping?

    all i want is to display all array values using the normal for loop not the foreach loop.
    like this Output.
    Java Code:
    Value is: 1
    Value is: 2
    Value is: 3
    Value is: 4
    Value is: 5
    Value is: 6
    Value is: 7
    Value is: 8
    Value is: 9
    Value is: 10
    Summation: 55
    in One JOptionPane.
    Last edited by raffs03; 06-16-2013 at 06:44 AM.

  12. #12
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: JOptionPane looping?

    You are getting an ArrayIndexOutOufBounds Exception because x after the loop is 10, and your array size is from 0 to 9, 10 is out of bounds.
    And even if x was a valid index in the array, it would just display the value at that position, not all the values.
    JOptionPane cannot be placed inside the loop because it will be initialized 10 times, so the kind of loop you want using a JOptionPane, I don't think that can work.
    Why won't you use the toString method of Arrays?

Similar Threads

  1. need some help in looping
    By toume in forum New To Java
    Replies: 5
    Last Post: 12-24-2012, 03:15 PM
  2. Looping: Help Please
    By michelle_323 in forum New To Java
    Replies: 5
    Last Post: 10-12-2011, 11:59 AM
  3. Looping
    By Dean29126 in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 02:01 PM
  4. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  5. Looping Help Please
    By JonnySnip3r in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 05:57 AM

Posting Permissions

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