Good evening, I am having a little trouble with the outcome of my bucket list assignment. Everything seems to be working just fine, but the final outcome isn't up to par. When you enter numbers from 1-30 or 61-99 it goes into its proper place, however numbers enter in the 31-60 range automatically goes into the 61-99 bucketlist. Perhaps the answer is simple, but I have been at this all day and could use a outsiders pov. I will cite the area you helped me in if you want. Here is the code:

import javax.swing.JOptionPane;

public class JavaAssignmentUnit3Draft2
  /*
   * The second draft focused more on the JOptionPane. 
   * Within the JOptionPane. The codes used is a mixture of previous codes
   * I used in past assignments and new codes.
   * Date Started 7/13/10 11:18 P.M. Status: incomplete... ended @ 12:30 A.M.
   * Final outcome: JOptionPane appears and can enter 10 integers. However,  
   * BarChart "*" appears to function normally for lower numbers 0-30.
   * Numbers enter to 31-60 automatically goes into 61-99. Considering creating a thrid draft 
   * Items to be added: -1 to quit program.
   */
{
  public static void main ( String[] args )
  {
    int[] array = new int[11]; //it takes numbers 0 - 99
      int num = 0;
      int count = 0;
      int total = 0;
      String input;
      for (count = 0; count < 11; 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 [ 0 ] = num;
      }
      System.out.println ( "The amount of times the numbers between 0 - 99 occured. Amount are displayed by *");
      String[] star = { "0-30 |", "31-60 |", "61-99 | "};
      for ( count = 0; count < 10; count++)
      {
        num = array [ count ];
        if ( num < 5 ) star [0] += "*";
        else if ( num < 10 ) star [1] += "*";
        else star [2] += "*";
      }
      for ( count = 0; count < 3; count++ )
        System.out.println( star[ count ] );
  }
}

Thank you ahead of time.

int[] array = new int[11]; //it takes numbers 0 - 99

Your comment here doesn't match the size of the array

I don't see any logic for the range of numbers you talk about: 1-30, 31-60 & 61-99

array [ 0 ] = num;

This always stores num at the first location in the array, replacing what was there.
What is the purpose of that?

You need to stop writing code for the program and design it first. Use a piece of paper and write down the steps needed to solve the problem. Then write the steps in pseudo code and THEN write the java program.
Your wasting your time trying to write a program without a design.

I did use a paper coding first and it looked good to me. It was array[count] = num and I changed it to [0]. Then I post the code here when I tried everything I could do.

Are you sure you posted the correct code. There isn't any logic for the numbers to be put into the ranges of numbers you mention.

It was array[count] = num and I changed it to [0].

Why did you do that? Did you look at your design on paper and see it should be that way?

NormR1 after stepping away and coming back to my old paper code I see what you ment. So I revised it a bit, but now I am having a new problem. The JOptionPane is only appearing once, instead of ten times for multiple entries. On paper it honestly looks like a cluster bomb. Again thank you for your response earlier. Here is my revised code, please keep in mind it is a third rough draft and is still working out the kinks. It compiles and runs when it does work (for a single number that is.)

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.
   * 
   * Items to be added: -1 to quit program.
   */
{
  public static void main ( String[] args )
  {
    int[] array = new int[11]; //it takes numbers 0 - 99
      int num = 0;
      int count = 0;
      int total = 0;
      String input;
      for (count = 0; count < 11; 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();
    }
  }
  }
}

You have 2 for loops one inside the other. And if you omit the code inside them you get something like this:

for (count=0;count<11;count++) {
  for (count=0;count<frequency.length;count++) {

  }
}

You are using the same variable to access two entire different arrays. And the fact that they might have the same length doesn't mean anything. When the inner loop finishes execution the count would have the value of frequency.length. Then its value will increase because of the outer loop and since it is not lower than 11 the outer loop will exit.

Try using this code from now on:

int[] array = new int[11];

for (int count=0;count<[B]array.length[/B];count++) {
  array[count]
  ....

  for (int k=0;k<[B]frequency.length[/B];k++) {
      frequency[k]
  }
}

Never hard code numbers.


Also another error:

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.
        }

num = Integer.parseInt(input)
input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ")

If the number is not between 1,99 then you should ask the user for a new number. But you first use the same input, which would be wrong, and then you ask for a new input.
Imagine entering -10, (input="-10", num=-10)
You go inside the while and after the error message, you give the num variable the same value:
num = Integer.parseInt(input) (input="-10", num=-10)
And then you ask for the user to enter a new value:
num = Integer.parseInt(input) (input="-10", num=-10)
input = JOptionPane.showInputDialog( "Enter a number between 0 - 99: ") (input="5", num=-10)
And then when you go at the beginning of the while loop the "input" has the new value but the "num" has the old.

You need first to ask from the user the new value, and then put that into the num.

Your code still doesn't appear related to the problem. Where is the references to the ranges of numbers 1-30-60-99?

Go back to the design stage and type in here your pseudo code for the program.
Lets get the logic done BEFORE you do any more coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.