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.
Code :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)); } } } }
