Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with an Array!

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with an Array!

    How do I get the variable checkNumber to read from the input and exit if the user enters a value less than 0? I initially created the array to accept 10 integer values and then average and sum them using JOptionPane. Now I would like the user to be able to exit the program if a negative value is entered in the input. Here's how it looks so far and I understand the if statement is where I am losing it.

    import java.text.NumberFormat;
    import java.text.DecimalFormat;
    import javax.swing.*;
     
    class originalArray
    {
        public static void main(String args[])
        {
            int checkNumber;
            int inputArray[] = new int[10]; 
            int arrayLength = inputArray.length;
            Boolean keepRunning = true;
     
                while (keepRunning)
                {
                    for(int i=0;i<arrayLength;i++) 
                    inputArray[i]=Integer.parseInt(JOptionPane.showInputDialog("Enter integer for array location "+i+" :")); 
                    //checkNumber = inputArray;
                    if (checkNumber <0)
                        {
                            System.exit(0);
                        }   
                    else           
                        {
                            int sum = 0;
                            String loc = "";
     
                            for(int i=0; i<arrayLength; i++)
                            {
                                sum+= inputArray[i];
                                loc+= "Array location " + i + " is " + inputArray[i] + "\n";
                            }
     
                                double avg;
                                NumberFormat formatter = new DecimalFormat ("#0.000");
                                avg =(double)sum/10;
     
     
                                JOptionPane.showMessageDialog (null, loc + "The sum or the array is " + sum + "\n The average of the array is " + formatter.format(avg));
                            }
                        }
                  }       
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help with an Array!

     inputArray[i]=Integer.parseInt(JOptionPane.showInputDialog("Enter integer for array location "+i+" :")); 
     if ( inputArray[i] < 0 ){
        return;//or System.exit(0);
    }

Similar Threads

  1. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  2. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  3. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM