The Artima Developer Community
Sponsored Link

Java Answers Forum
Pass the array please..

1 reply on 1 page. Most recent reply: Oct 15, 2009 1:54 AM by xinzhi zhang

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
mardhiah bakri

Posts: 3
Nickname: mardhiah21
Registered: Sep, 2009

Pass the array please.. Posted: Sep 30, 2009 8:41 AM
Reply to this message Reply
Advertisement
hi!
i'm having problem with this programming..can anyone check where is my error..

Question:
Write a program called PassTheArrayPlease.java that asks the user to enter five numbers and then stores them in an array. When finished receiving the numbers, the program should pass the array to a method called averageNumbers. This method should average the numbers and return the average to main. The method main should then display the result to the user. Use the class NumberFormat to format the average to three decimal places.

this is my answer:
import javax.swing.JOptionPane;

public class PassTheArrayPlease
{
public static void main(String[] args)
{
String s;
s = JOptionPane.showInputDialog(null, "Enter five numbers");
double[] numbers = new double[5];

averageNumbers(numbers);

JOptionPane.showMessageDialog(null, "Average: " + result/numbers.length);
}

public static double averageNumbers(double result, double average, double[] numbers)
{
result = 0.0;

for(int i=0; i<numbers.length; i++)
{
result = result + numbers;
}

return result;
}
}

-and also how to use the class NumberFormat to format the average to three decimal places?


xinzhi zhang

Posts: 2
Nickname: zhangxzhi
Registered: Sep, 2009

Re: Pass the array please.. Posted: Oct 15, 2009 1:54 AM
Reply to this message Reply
public class Main {

private static boolean fillInput(double[] numbers, String input) {
if (input == null || input.length() == 0)
return false;
String[] s = input.split(" ");
if (s == null || s.length != 5)
return false;
try {
for (int i = 0; i < s.length; i++) {
numbers[i] = Double.parseDouble(s[i]);
}
} catch (NumberFormatException e) {
return false;
}
return true;
}

private static double averageNumbers(double[] numbers) {
if (numbers == null || numbers.length == 0)
return 0;

double total = 0.0;

for (int i = 0; i < numbers.length; i++) {
total = total + numbers[i];
}

return total / numbers.length;
}

public static void main(String[] args) {

double[] numbers = new double[5];

String s = null;

while (!fillInput(numbers, s)) {
s = JOptionPane.showInputDialog(null,
"Enter five numbers, use space to separate them.");
}

double average = averageNumbers(numbers);

JOptionPane.showMessageDialog(null, "Average: " + average);
}

}

Flat View: This topic has 1 reply on 1 page
Topic: HELP!!! input = keyboard.nextLine(); Previous Topic   Next Topic Topic: BeansBinding issue

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use