473,569 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 8142
jkmyoung
2,057 Recognized Expert Top Contributor
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
25547
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 made by normal php code and then submitted between form-tags as echo "<input type='hidden' name=\"arrayname\" value=\"$arrayname\" />" ;
1
2116
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" is an option in the language dropdown, but selecting it does nothing. This seems to be isolated to my application -- other applications let me...
1
4732
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). Having the application to write the array to a DB2 global temp table, or passing it in any surrogate form like delimited string (or a byte stream)...
4
5797
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 jdk11_path to: Java Development Kit 1.1 installation path (JDK11_PATH) =
6
2273
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 column. And the last column is the sum. Input should stop when zero is entered. Zero shouldnt be output. Empty cells should be output to complete the...
2
6859
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.... thanks phil
1
4052
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
2507
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 int; <= the problem seem like happen at here? private int arrayB = new int; <= the problem seem like happen at here?
1
1872
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 EOF" way? Am not much of a java programmer but I need this one. I can connect to my sql server and query the table. I just need to know how to place...
2
3740
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 when i do the following. if you add a contact with up to 13 fields it will be stored in a data structure. i have a tabbed pane that will show six of...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6286
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.