problems with JOptionPane

JOptionPane input box appearing once instead of ten times for barchart

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2546 Views - Last Post: 17 July 2010 - 08:56 PM Rate Topic: -----

#1 feastfulsaint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-July 10

problems with JOptionPane

Posted 15 July 2010 - 08:30 PM

I have been on this all day and a bit burnt out right now. This is my third draft for a barchart. It compiles, the the JOptionPane appears once. It supposed to appear ten times for multiple integer entries. Here is the code:

Code:

import javax.swing.JOptionPane;

public class JUnit3Draft3
  /* Work log:
   * The third draft is a mixture of draft two and some new coding
   * However the JOptionPane won't allow me to enter 10 integers, just one.
   * Started paper coding @ 10:15 Am. Ended paper coding @ 4:22 pm
   * Began physical coding @ 7:07 PM. Ended physical coding @ 7:44 pm
   * Outcome: JOptionPane appears once, but outcome is successful.
   * Error message working. Reworked code a large number of ways, but 
   * JOptionPane stills appears only once. Submitted project for peer review.
   * Items to be added: -1 to quit program.
   */
{
  public static void main ( String[] args )
  {
      int num = 0;
      int count = 0;
      String input;
      {
        input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        num = Integer.parseInt(input);
        while (num < 0 || num > 99)
        {
          JOptionPane.showMessageDialog( null, "Must be between 0 and 99",
                                        "ERROR!", JOptionPane.ERROR_MESSAGE );
          num = Integer.parseInt(input);
         input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        }
        array [ count ] = num;

    System.out.println( "number average:" );
    int[] frequency = new int[11];
   
      ++frequency[ num / 10 ];
    for ( count = 0; count < frequency.length; count++)
    {
      if (count == 10)
        System.out.printf( "%5d: ", 100);
      else
        System.out.printf( "%02d-%02d: ",
                          count * 10, count * 10 + 9 );
      for ( int stars = 0; stars < frequency[ count ]; stars++ )
        System.out.print("*");
      System.out.println();
    }
  }
  }
}


I think the answer is simple, but again I am burnt out. Thank you ahead of time.

Is This A Good Question/Topic? 0
  • +

Replies To: problems with JOptionPane

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: problems with JOptionPane

Posted 15 July 2010 - 08:38 PM

Put your promting JOptionPane in a loop
Was This Post Helpful? 0
  • +
  • -

#3 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 193
  • Joined: 26-October 09

Re: problems with JOptionPane

Posted 15 July 2010 - 08:41 PM

It looks to me to be missing a counter. Something like
int counter = 0;
        while(counter < 9){
then ask for the input and keep asking to fill the array to 10.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: problems with JOptionPane

Posted 15 July 2010 - 08:44 PM

this will prompt the user only once

      String input;
      {
        input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        num = Integer.parseInt(input);
        while (num < 0 || num > 99)
        {
          JOptionPane.showMessageDialog( null, "Must be between 0 and 99",
                                        "ERROR!", JOptionPane.ERROR_MESSAGE );
          num = Integer.parseInt(input);
         input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        }
        array [ count ] = num;

}

may be you need something like
      String input;
      for(count = 0; count < 10; count++)
      {
        input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        num = Integer.parseInt(input);
        while (num < 0 || num > 99)
        {
          JOptionPane.showMessageDialog( null, "Must be between 0 and 99",
                                        "ERROR!", JOptionPane.ERROR_MESSAGE );
          num = Integer.parseInt(input);
         input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        }
        array [ count ] = num;
       }

}

Was This Post Helpful? 0
  • +
  • -

#5 feastfulsaint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-July 10

Re: problems with JOptionPane

Posted 15 July 2010 - 08:54 PM

View PostMKunstman, on 15 July 2010 - 07:41 PM, said:

It looks to me to be missing a counter. Something like
int counter = 0;
        while(counter < 9){
then ask for the input and keep asking to fill the array to 10.

I have tried it, and yes the JOptionPane does appears more than once. However, after every entry the list of numbers keeps going instead of storing and giving me a final outcome. Again thank you.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: problems with JOptionPane

Posted 15 July 2010 - 09:12 PM

View Postfeastfulsaint, on 15 July 2010 - 09:54 PM, said:

View PostMKunstman, on 15 July 2010 - 07:41 PM, said:

It looks to me to be missing a counter. Something like
int counter = 0;
        while(counter < 9){
then ask for the input and keep asking to fill the array to 10.

I have tried it, and yes the JOptionPane does appears more than once. However, after every entry the list of numbers keeps going instead of storing and giving me a final outcome. Again thank you.

doesn't my previous reply fixed your problem ?
Was This Post Helpful? 0
  • +
  • -

#7 feastfulsaint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-July 10

Re: problems with JOptionPane

Posted 16 July 2010 - 04:27 AM

View Postpbl, on 15 July 2010 - 07:44 PM, said:

this will prompt the user only once

      String input;
      {
        input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        num = Integer.parseInt(input);
        while (num < 0 || num > 99)
        {
          JOptionPane.showMessageDialog( null, "Must be between 0 and 99",
                                        "ERROR!", JOptionPane.ERROR_MESSAGE );
          num = Integer.parseInt(input);
         input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        }
        array [ count ] = num;

}

may be you need something like
      String input;
      for(count = 0; count < 10; count++)
      {
        input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        num = Integer.parseInt(input);
        while (num < 0 || num > 99)
        {
          JOptionPane.showMessageDialog( null, "Must be between 0 and 99",
                                        "ERROR!", JOptionPane.ERROR_MESSAGE );
          num = Integer.parseInt(input);
         input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        }
        array [ count ] = num;
       }

}

Oh I see, I will give it a try.
Was This Post Helpful? 0
  • +
  • -

#8 feastfulsaint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-July 10

Re: problems with JOptionPane

Posted 16 July 2010 - 08:45 AM

sorry pbl I am getting an error. Cannot find symbol.
public static void main ( String[] args )
  {
      int num = 0;
      int count = 0;
      String input;
     for(count = 0; count < 10; count++)
          {
        input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        num = Integer.parseInt(input);
        while (num < 0 || num > 99)
        {
          JOptionPane.showMessageDialog( null, "Must be between 0 and 99",
                                        "ERROR!", JOptionPane.ERROR_MESSAGE );
          num = Integer.parseInt(input);
         input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");//Suggested by professor Imroz.
        }
        array [ count ] = num;
     }
    System.out.println( "number average:" );
    int[] frequency = new int[11];
   
      ++frequency[ num / 10 ];
    for ( count = 0; count < frequency.length; count++)
    {
      
      if (count == 10)
        System.out.printf( "%5d: ", 100);
      else
        System.out.printf( "%02d-%02d: ",
                          count * 10, count * 10 + 9 );
      for ( int stars = 0; stars < frequency[ count ]; stars++ )
        System.out.print("*");
      System.out.println();
    }
  }

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: problems with JOptionPane

Posted 16 July 2010 - 08:46 AM

What line is giving you this error? Did you import the JOptionPane class?
Was This Post Helpful? 0
  • +
  • -

#10 feastfulsaint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-July 10

Re: problems with JOptionPane

Posted 16 July 2010 - 03:13 PM

View Postmacosxnerd101, on 16 July 2010 - 07:46 AM, said:

What line is giving you this error? Did you import the JOptionPane class?

Yes I have imported the JOptionPane, I am trying to appear ten times, store the integer and then display the outcome in a barchart.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: problems with JOptionPane

Posted 16 July 2010 - 03:32 PM

View Postfeastfulsaint, on 16 July 2010 - 09:45 AM, said:

sorry pbl I am getting an error. Cannot find symbol.

Which symbol at which line ?
Was This Post Helpful? 0
  • +
  • -

#12 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: problems with JOptionPane

Posted 16 July 2010 - 03:34 PM

What line does the error occur? Copying and pasting the error can help to solve the problem, since for all you know it might be your 'barchart' implementation that could be wrong.
Was This Post Helpful? 0
  • +
  • -

#13 feastfulsaint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-July 10

Re: problems with JOptionPane

Posted 16 July 2010 - 04:08 PM

View Postm-e-g-a-z, on 16 July 2010 - 02:34 PM, said:

What line does the error occur? Copying and pasting the error can help to solve the problem, since for all you know it might be your 'barchart' implementation that could be wrong.


I added int[] array = new int[11], and it work 'sort of. The JOptionPane appears 10 times, but the barchart won't work properly.
Again thank you all for helping me.

This post has been edited by feastfulsaint: 16 July 2010 - 04:11 PM

Was This Post Helpful? 0
  • +
  • -

#14 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: problems with JOptionPane

Posted 16 July 2010 - 04:15 PM

Why call an array 'array'? Good practice to name the array relevant to your code. Post your full code so I can see the specific problem.

This post has been edited by m-e-g-a-z: 16 July 2010 - 04:16 PM

Was This Post Helpful? 0
  • +
  • -

#15 feastfulsaint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-July 10

Re: problems with JOptionPane

Posted 16 July 2010 - 04:22 PM

View Postm-e-g-a-z, on 16 July 2010 - 03:15 PM, said:

Why call an array 'array'? Good practice to name the array relevant to your code. Post your full code so I can see the specific problem.

import javax.swing.JOptionPane;

public class JavaAssignmentUnit3Draft3
  /*
   * The third draft is a mixture of draft two and some new coding
   * However the JOptionPane won't allow me to enter 10 integers, just one.
   * Started paper coding @ 10:15 Am. Ended paper coding @ 4:22 pm
   * Began physical coding @ 7:07 PM. Ended physical coding @ 7:44 pm
   * Outcome: JOptionPane appears ten times, but only last integer enterd is taken towards barChart.
   * Error message working. 
   * Items to be added: -1 to quit program.
   */
{
  public static void main ( String[] args )
  {
    int[] array = new int[11];
      int num = 0;
      int count = 0;
      String input;
     for(count = 0; count < 10; count++)
          {
        input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        num = Integer.parseInt(input);
        while (num < 0 || num > 99)
        {
          JOptionPane.showMessageDialog( null, "Must be between 0 and 99",
                                        "ERROR!", JOptionPane.ERROR_MESSAGE );
          num = Integer.parseInt(input);
         input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ");
        }
        array [ count ] = num;
     }
    System.out.println( "number average:" );
    int[] frequency = new int[11];
   
      ++frequency[ num / 10 ];
    for ( count = 0; count < frequency.length; count++)
    {
      
      if (count == 10)
        System.out.printf( "%5d: ", 100);
      else
        System.out.printf( "%02d-%02d: ",
                          count * 10, count * 10 + 9 );
      for ( int stars = 0; stars < frequency[ count ]; stars++ )
        System.out.print("*");
      System.out.println();
    }
  }
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2