Results 1 to 5 of 5
  1. #1
    jamjam is offline Member
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Hi everyone! Help with JOptionPane and user input

    I have a question about this array. I'd like to use a JOptionpane to get the input to change the number of students to add to this array.
    It works like it is when I use the DOS shell command. Arrays are kinda my weak spot. Thanks!

    Java Code:
    String input = JOptionPane.showInputDialog(null, "Enter number of students here");
            int students = Integer.parseInt(input);
            Student[] stu = new Student[Integer.parseInt(input)];	//	Set up the array using argument
            for (int i = 0; i < stu.length; i++) {
                stu[i] = new Student("Name" + i, 
                          (int) (Math.random() * 1000000) % 20 + 1970, "Computer Science", (int) (Math.random() * 1000000),
                          (int) (Math.random() * 1000000) % 4 + 1);
                if (stu[i].getStatus() == 3) {
                    msg += stu[i].toString() + '\n';
                }
          
            }
          
            
         
            
            javax.swing.JOptionPane.showMessageDialog(null, msg, "Persons", javax.swing.JOptionPane.INFORMATION_MESSAGE);
    Last edited by jamjam; 05-05-2013 at 04:52 AM.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Hi everyone! Help with JOptionPane and user input

    for a start you need to know about other types of JOptionPane, specifically ones for getting user input

    [code]
    String input = JOptionPane.showInputDialog(null, "Enter number of students here");
    int students = Interger.parseInt(input);
    [code]

    this is what you will need, just adjust your code to cater for the above, shouldn't be too difficult!

  3. #3
    jamjam is offline Member
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Re: Hi everyone! Help with JOptionPane and user input

    Thank you! I appreciate the help. I got it working somewhat but now I have another problem.
    The input JOptionPane comes up and I put in the number say "44" but instead of getting 44 students, I get about 7 students.
    Like I said I'm really newbie with arrays so I'm trying really hard to wrap my head around it but its not clicking yet.

    EDIT: I think its all working right now. I believe the reason is because it only shows students with a status that is equal to 3.
    Can I get a double confirm?

    Here is my new code

    Java Code:
    String input = JOptionPane.showInputDialog(null, "Enter number of students here");
            int students = Integer.parseInt(input);
            Student[] stu = new Student[Integer.parseInt(input)];	//	Set up the array using argument
            for (int i = 0; i < stu.length; i++) {
                stu[i] = new Student("Name" + i, 
                          (int) (Math.random() * 1000000) % 20 + 1970, "Computer Science", (int) (Math.random() * 1000000),
                          (int) (Math.random() * 1000000) % 4 + 1);
                if (stu[i].getStatus() == 3) {
                    msg += stu[i].toString() + '\n';
                }
          
            }
          
            
         
            
            javax.swing.JOptionPane.showMessageDialog(null, msg, "Persons", javax.swing.JOptionPane.INFORMATION_MESSAGE);
    Last edited by jamjam; 05-05-2013 at 05:00 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Hi everyone! Help with JOptionPane and user input

    Java Code:
    if (stu[i].getStatus() == 3) {
        msg += stu[i].toString() + '\n';
    }
    Yes - only students with a status of 3 are generating something in the message, although all the students are being added to the array.

    If in doubt about how many students are being added you can System.out.println("size of array is " + stu.length);

  5. #5
    jamjam is offline Member
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Re: Hi everyone! Help with JOptionPane and user input

    Quote Originally Posted by pbrockway2 View Post
    Yes - only students with a status of 3 are generating something in the message, although all the students are being added to the array.

    If in doubt about how many students are being added you can System.out.println("size of array is " + stu.length);
    Thank you so much. cheers!

Similar Threads

  1. Replies: 2
    Last Post: 10-20-2011, 03:43 PM
  2. Replies: 5
    Last Post: 08-15-2011, 07:10 PM
  3. Converting Scanner input to JOptionPane
    By Mideoan in forum New To Java
    Replies: 1
    Last Post: 03-10-2011, 12:26 AM
  4. Input JOptionPane
    By nic21 in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 06:32 PM
  5. JOptionPane - input dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 10: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
  •