The Artima Developer Community
Sponsored Link

Java Answers Forum
Checking the arrays then adding and averaging...

4 replies on 1 page. Most recent reply: Dec 2, 2004 4:37 AM by Alan Fung

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 4 replies on 1 page
Alan Fung

Posts: 5
Nickname: afu
Registered: Dec, 2004

Checking the arrays then adding and averaging... Posted: Dec 2, 2004 1:43 AM
Reply to this message Reply
Advertisement
I am reading 6 strings from the user and then putting it in an array. I don't know how to check if the strings are numbers and if they are numbers how do you adding them together and finding the average of the numbers?

I am so lost, help is MUSH appreciated. Thank you.


Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: Checking the arrays then adding and averaging... Posted: Dec 2, 2004 2:01 AM
Reply to this message Reply
Here is the code for it.

public class FindAverage {
public static void main(String args[]) {
String[] userValues = {"abc", "xyz", "1", "34", "45", "jkl"};
int totalSum = 0;
int elementCount = 0;
for (int i = 0; i < userValues.length; i ++) {
try {
totalSum += (Integer.parseInt(userValues));
elementCount++;
} catch (NumberFormatException nfe) {
System.out.println(userValues + " is not an integer.");
}
}
System.out.println("Sum = " + totalSum);
System.out.println("Elements = " + elementCount);
float avg = totalSum/elementCount;
System.out.println("Average = " + avg);
}
}

Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: Checking the arrays then adding and averaging... Posted: Dec 2, 2004 2:06 AM
Reply to this message Reply
There were some elements missing in my previous code snippet. You can use this code instead...




public class FindAverage {
public static void main(String args[]) {
String[] userValues = {"abc", "xyz", "1", "34", "45", "jkl"};
int totalSum = 0;
int elementCount = 0;
for (int i = 0; i < userValues.length; i ++) {
try {
totalSum += (Integer.parseInt(userValues));
elementCount++;
} catch (NumberFormatException nfe) {
System.out.println(userValues + " is not an integer.");
}
}
System.out.println("Sum = " + totalSum);
System.out.println("Elements = " + elementCount);
float avg = totalSum/elementCount;
System.out.println("Average = " + avg);
}
}


Rajeev

Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: Checking the arrays then adding and averaging... Posted: Dec 2, 2004 2:12 AM
Reply to this message Reply
Hi Alan,

in couple of places where u r trying to read the userValues the sqaurebrace i squarebraceclose are missing. U need to add them after userValues in the for loop and excetpion handling part. Then the code will work fine. There is some problem with artima editor, its not taking squarebraceopen i squarebraceclose \[i\]

public class FindAverage {
public static void main(String args[]) {
String[] userValues = {"abc", "xyz", "1", "34", "45", "jkl"};
int totalSum = 0;
int elementCount = 0;
for (int i = 0; i < userValues.length; i ++) {
try {
totalSum += (Integer.parseInt(userValues\[i\]));
elementCount++;
} catch (NumberFormatException nfe) {
System.out.println(userValues\[i\] + " is not an integer.");
}
}
System.out.println("Sum = " + totalSum);
System.out.println("Elements = " + elementCount);
float avg = totalSum/elementCount;
System.out.println("Average = " + avg);
}
}

Alan Fung

Posts: 5
Nickname: afu
Registered: Dec, 2004

Re: Checking the arrays then adding and averaging... Posted: Dec 2, 2004 4:37 AM
Reply to this message Reply
Thank you very much.

Flat View: This topic has 4 replies on 1 page
Topic: Using Javabeans with Tomcat Previous Topic   Next Topic Topic: Java Menu

Sponsored Links



Google
  Web Artima.com   

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