?
Solved

Java Applet with arrays

Posted on 2003-03-12
7
Medium Priority
?
264 Views
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

Wesmk
0
Comment
Question by:wesmk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 35

Accepted Solution

by:
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.
0
 

Expert Comment

by:JackNCalvin
ID: 8122886
Wesmk:

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!

-Jack-
0
 

Expert Comment

by:SeaCosmos
ID: 8124596
if you just wanna add the number to the array.you can do this way:
  ...
  //class scope varible
  double[] doubleArray = null;
  ...
 
  //method
  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.


0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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!

Mayank.

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

Expert Comment

by:CleanupPing
ID: 9058733
wesmk:
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 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:girionis
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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