473,419 Members | 4,250 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Java array that gets sum, mean, highest num, lowest num, and exits on input Zero

1
I have been working on the following code for about 6 hours now. I am attempting to write a program in Java that finds the highest, lowest, sum, and mean of an array. The program should also end inputs with the input of "0." I have figured out how to do the sum, mean, highest, lowest. The issue I'm having is to get the array count and to make it exit on "0." Is there a way to have the array variable outside the do/while control statement that will also allow me to use the "array" counter inside of it?

Edit: Also, this program should not read the 0 for it's output as lowest.

Expand|Select|Wrap|Line Numbers
  1. package array;
  2. import javax.swing.JOptionPane;
  3.   public class TestArray{
  4.      public static void main(String[] args) {
  5.  
  6.         // Initialize array
  7.          int array = 1;
  8.          int[] numbers = new int[array];
  9.  
  10.         // Create input
  11.       do   for (int i = 0; i < array; i++)
  12. String numString = JOptionPane.showInputDialog(null,
  13.                      "Enter  number:",
  14.                      "Example 5.1 Input",
  15.                  JOptionPane.QUESTION_MESSAGE);
  16.  
  17.              numbers[array] = Integer.parseInt(numString);
  18.  
  19.       }
  20.              while (array != 0);
  21.  
  22.         int max = numbers[0];
  23.          for (int i = 1; i < array; i++) {
  24.              if (max < numbers[i])
  25.                  max = numbers[i];
  26.          }
  27.         int min = numbers[0];
  28.         for (int i =0; i < array; i++) {
  29.             if (min > numbers[i])
  30.                 min = numbers[i];
  31.         }
  32.         String output = "RESULTS ";
  33.  
  34.               int sum = 0;
  35.             for (int i = 0; i < array; i++)
  36.                 sum += numbers[i];
  37.             double average = sum/array;
  38.             output += "\nThe total is " + sum;
  39.             output += "\nThe average is " + average;
  40.  
  41.         output += "\nThe largest number is " + max;
  42.         output += "\nThe smallest number is " + min;
  43.         output += "\nThe number of entries is " + array;
  44.         JOptionPane.showMessageDialog(null, output,
  45.                 "Example 5.1 Output", 
  46.         JOptionPane.INFORMATION_MESSAGE);
  47.         System.exit (0)
  48.       }
  49.      }
  50.  
May 31 '10 #1
1 8134
jkmyoung
2,057 Expert 2GB
I think you want
while (numbers[array]!=0)
May 31 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: pablo | last post by:
Dear News Groupers, I'am trying to send a php array with a hidden input field from a form to another script. The array is NOT made directly by way of <input name="arrayname" />. The array is...
1
by: Hegemony Cricket | last post by:
I have a java application that I'm trying to run on a Mac (Powerbook G5). When trying to enter Chinese text into a text field, it's not allowing me to switch input methods. "Simplified Chinese"...
1
by: Eugene | last post by:
Hi All, I am having a hard time figuring out how to pass a ("true") java array, like int, from a jdbc application in to a DB2 stored procedure or UDF (can be a java or SQL proc or a table UDF)....
4
by: acemann7 | last post by:
Windows 2000 udb v 7.2 Got the latest fixpak 13 Applied it. Still can't get a single java sp to run. I used to get reason "2"... now reason "0" - reason 0 is not even documented. I point...
6
by: gdarian216 | last post by:
I need help with my homework....i have to write a program that inputs multiple lines of integer numbers and outputs them in an HTML table. The numbers should be printed 3 in a row, one number per...
2
by: phil.swenson | last post by:
I'm using Prototype.js and would like to convert the contents of an HTML table to JSON. Converting to an array first is fine too. Any thoughts on this? I haven't seen anyone do anything this.......
1
by: pompeez | last post by:
Hi, I need to write a Java Array/ArrayList/List to a text file in Jython. I used the .write() function, but it is letting me write only a string. Thanks! Pompeez
5
by: xirowei | last post by:
public class Result { private int countA = 0; private int countB = 0; private int statement; private boolean statusA = false; private boolean statusB = false; private int arrayA = new...
1
ck9663
by: ck9663 | last post by:
Hi guys, CK here from SQL Server forum. Is there a function that will automatically populate a two-dimensional array coming from a SQL Server table? Or should I do it the old fashion "WHILE NOT...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.