Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Java Applet with arrays

Posted on 2003-03-12
Medium Priority
Last Modified: 2010-03-31
I'm trying to get real numbers entered from user (max 20) and put them in an array to sort ascending and descending also to find the mean, median, and range.  I know how to do all that, I just can't figure out how to put the numbers entered into the array.  I'm using GUI based to get input from users (valueEntered = JOptionPane.showInputDialog ("Enter a floating-point value, end to exit:" );) then converting it to double.  I don't know how to get those numbers into my array so I can do all the above.  thanks for any help

Question by:wesmk
LVL 35

Accepted Solution

girionis earned 100 total points
ID: 8122849
 You need a while loop but you need to predefine your array length since arrays cannot expand dynamically. Lets say for an array with 10 elements you could so something like:

  float [] values = new float[10]
  for (int i=0; i<10; i++)
    valueEntered = JOptionPane.showInputDialog ("Enter a floating-point value, end to exit:" );
    velues[i] = Float.parseFloat(valueEnterd);

  Hope it helps.

Expert Comment

ID: 8122886

I think it is a bit insecure to use an array for what you are trying to do.  You might want to consider using a vector, which grows dynamically.  But if you know for a fact that the max number is 20, you would benifit from using arrays for the sorting algorithms.

If you use an array you must keep track of how many numbers have been placed into the array.

Here is an example:

private int numberOfValues = 0; // int counter for array
double userValues[] = new double[20]; // Users values
double userInput;  // Value received from GUI

now... each time the user enters a value, you can do something like this:

if (numberOfValues < 20) {

   userValues[numberOfValues]= UserInput;
   numberOfValues++;  // increment number of Values

} else {System.out.println ("Can not enter any more values!");}

If I understand what I think you are asking, this is the most basic way to solve your problem.

I hope this helps!


Expert Comment

ID: 8124596
if you just wanna add the number to the array.you can do this way:
  //class scope varible
  double[] doubleArray = null;
  public void addDoubleToArray( double doubleValue ) {
      if( doubleArray == null ) {
          doubleArray = new double[ 1 ];
          doubleArray[ 0 ] = doubleValue;
      } else {
          int orgLenth =  doubleArray.length;
          int[] tempArray = new int[ orgLenth + 1 ];
          for( int i = 0; i < tempArray.length; i++ ) {
              if( i == tempArray.length - 1 ) {
                  tempArray[ i ] = doubleValue;
              } else {
                  tempArray[ i ] = doubleArray[ i ];
          doubleArray = tempArray;

but as JackNCalvin said,it's insecure.you'd better synchronize the Array because in GUI event processing,it's multi-thread based.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 30

Expert Comment

by:Mayank S
ID: 8125358
Since you have a max. of 20 numbers and you also have to stop in case the user types "end":

float values[] = new float[20] ;
int count ;
String str ;

for ( count = 0 ; count < 20 ; count ++ )
  str = JOptionPane.showInputDialog ( "Enter a floating-point value, end to exit: " ) ;

  if ( str.equals ( "end" ) )
    break ; // end if

  // else - copy the value to the array

  values[count] = Float.parseFloat ( str ) ;
  count ++ ;

} // end for

Now, the numbers in the array can be accessed as:

for ( int i = 0 ; i < count ; i ++ )
  // use 'values[i]' to access the numbers

That'll do it!


PS: Try putting a try-catch block outside the given code as the parseFloat () method can throw a NumberFormatException if the number-format is invalid. We can help you out with that if you're having problems with it.

Expert Comment

ID: 9058733
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
LVL 35

Expert Comment

ID: 9714454

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to girionis

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question