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 this to end when a negative number is entered

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

    Default Need this to end when a negative number is entered

    I'm hung up here on how to get this to end when a negative number is entered. I want the user to enter 10 integers into an array. After those are entered, the program will sum and average the data. Inputs and outputs use JOptionPane. When a negative number is entered, the program should end. That's where I'm stuck. The while statement is what I can't seem to figure out. Any pointers? Have I given enough information for what I am asking?

    import java.text.NumberFormat;
    import java.text.DecimalFormat;
    import javax.swing.*;
     
    public class Array
    {
        public static void main(String args[])
        {
            int checkNumber =0;
            int inputArray[] = new int[10]; 
            int arrayLength = inputArray.length;
            Boolean keepRunning = new Boolean (true);
     
                for(int i=0;i<arrayLength;i++) 
                checkNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter integer for array location "+i+" :")); 
     
                while (checkNumber>=0)
            {
                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 this to end when a negative number is entered

    if ( checkNumber < 0 ){
    return;
    }

Similar Threads

  1. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 07:01 AM
  2. Reverse Number
    By java1 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 28th, 2009, 10:19 AM
  3. Evaluating to negative zero
    By helloworld922 in forum Java Theory & Questions
    Replies: 6
    Last Post: June 25th, 2009, 02:34 PM
  4. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM
  5. [SOLVED] How to make a integer negative if it meets a certain criteria?
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 14th, 2009, 02:27 PM