Results 1 to 14 of 14
  1. #1
    Reero8532 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default How to display array integer in JOptionPane message dialog ?

    I am currently mind boggled on part of my homework assignment that I have been working on. Here is a link of my assignment, steps 1 through 9 is what I'm currently working on.

    Java Homework 7

    I mainly need help on trying to figure how to get an array of testGrades into a single Dialog Box. When I use a for loop it shows each individual grade in there own dialog box.

    Its ok that each grade are all zero's (0) for now, I just need to show that I can get every grade in a single dialogue box. Inputting the integers will be the second part of the assignment.

    ANY HELP IS APPRECIATED.... here is what I have so far



    import java.util.Scanner;
    import javax.swing.JOptionPane;


    public class Homework7 {

    public static void main(String []args)
    {
    String s;
    int numTests;

    s = JOptionPane.showInputDialog(null, "How many test grades have you given?");
    numTests = Integer.parseInt(s);

    JOptionPane.showMessageDialog(null, "The number of tests given is: " + numTests);

    int [] myGrades = new int [numTests];

    for (int i=0; i<=numTests; i++)
    {
    JOptionPane.showMessageDialog(null, myGrades[i]);
    }

    } //end of main method

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    One way would be to create a JTextArea. Add each grade as a new line in the text area. Then you can add the text area to the JOptionPane.

  3. #3
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Or you could iterate through the array and add all the integers to a single String, then call JOptionPane with the String

  4. #4
    Reero8532 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    could you show me an example on how to get an array into a single String, because I'm still trying to the hang of all the basics to JAVA.

  5. #5
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Java Code:
    StringBuffer result = new StringBuffer ();
    
    for (int i = 0; i < array.length; i ++) {
      result.append (array [i]).append (" ");
    }
    
    String theStringToShow = result.toString ();

  6. #6
    Reero8532 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    I took the example you gave me but I don't think I implemented it correctly, can you please tell me what I did wrong?


    import java.util.Scanner;
    import javax.swing.JOptionPane;


    public class Homework7 {

    public static void main(String []args)
    {
    String s;
    int numTests;

    s = JOptionPane.showInputDialog(null, "How many test grades have you given?");
    numTests = Integer.parseInt(s);

    JOptionPane.showMessageDialog(null, "The number of tests given is: " + numTests);

    int [] myGrades = new int [numTests];

    StringBuffer result = new StringBuffer ();

    for (int i=0; i<=myGrades.length; i++)
    {
    result.append (myGrades[i]).append (" ");

    }

    String theStringToShow = result.toString();

    JOptionPane.showMessageDialog(null, theStringToShow);

    } //end of main method

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,413
    Rep Power
    20

    Default

    No, you tell us what's wrong.

    -- Compile error?
    -- Runtime error?
    -- Runs but doesn't give the expected output? --> tell us what output you expected and what you got
    -- Computer goes up in a puff of green smoke?

    db

  8. #8
    Reero8532 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    I'm trying to get the array in the for loop and convert it to a String so I can put the results of the enitire array into a single dialog box. Instead of having one number per dialog box.

  9. #9
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    That's what you want to do, what's the problem you are seeing in your latest changes?

  10. #10
    Reero8532 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    Well iluxa suggested I iterate the integers in the array list to a String, so he gave me an example above on how to do it. So once I implemented it to my program, it didn't even show a dialog box of the inputted number of tests in the array. So I'm trying to figure out if I inputted the example he gave me in correctly or not. To be honest I'm not sure that I did.

    Here is what the output should look like, so you can have a better idea of what I'm trying to do:

    (This has to be done using ONLY DIALOGUE BOXES)

    Input Dialog Box: How many tests grades have you given? _____(Input number of tests)

    Dialog Box: The number of tests given is: 6 (show inputted number of tests, for example 6)

    Dialog Box: 0 (All the zero's have to be in a single dialog box, NOT one number per dialog box)
    0
    0
    0
    0
    0


    So the best way to approach this is to supposedly change the array integers into a String so the dialog box can show all of the numbers listed in the array. So if you know I can do this, could you please show me how I could implement this into my java code, because I'm still a bit new to JAVA.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Java Code:
    int[] array= {1, 2, 3, 4};
    String toShowInPane= Arrays.toString(array);
    kind regards,

    Jos

  12. #12
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Using a String the text will appear on a single line, not on multiple lines as you have shown in your drawing. I gave you a solution to have multiline display. There are other options you could use the array to create a JList and show it in the JOptionPane as well. Read the Swing tutorial for examples of using Swing compnents.

  13. #13
    Reero8532 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    Could you please show me how I would input that into my Java code, I'm not sure how to use it.

  14. #14
    Reero8532 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default [SOLVED] How to display array integer in JOptionPane message dialog ?

    Quote Originally Posted by JosAH View Post
    Java Code:
    int[] array= {1, 2, 3, 4};
    String toShowInPane= Arrays.toString(array);
    kind regards,

    Jos
    Here is my updated code with the array converted to a String
    Java Code:
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    public class Homework7 {
    
        public static void main(String []args)
        {
        String s;
        int numTests;
    
        s = JOptionPane.showInputDialog(null, "How many test grades have you given?");
        numTests = Integer.parseInt(s);
    
        JOptionPane.showMessageDialog(null, "The number of tests given is: " + numTests);
    
        int [] myGrades = new int [numTests];
    
        for (int i=0; i<=myGrades.length; i++)
            {
            }
    
        [B]String toShowInPane = Arrays.toString(myGrades);[/B]
    
        JOptionPane.showMessageDialog(null, [B]toShowInPane[/B]);
    
        } //end of main method
    Thanks a lot JosAH your a lifesaver!!!!!:)

Similar Threads

  1. Default Focus in JOptionPane Dialog
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 5
    Last Post: 11-11-2009, 02:59 PM
  2. Default Focus in JOptionPane Dialog
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 1
    Last Post: 11-09-2009, 12:15 PM
  3. JOptionPane dialog (Localizing)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 11:36 AM
  4. JOptionPane - message dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 09:11 AM
  5. JOptionPane - input dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 09:09 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
  •