Results 1 to 10 of 10
Thread: Need help with an error
 04172012, 04:26 AM #1Member
 Join Date
 Apr 2012
 Posts
 5
 Rep Power
 0
Need help with an error
code:
public static void main(String[] args) {
String numberSetsString = args[0]; // we assume there is always a number set passed in
String numberSetsWithoutBraces = numberSetsString.substring(1, numberSetsString.length() 1); // remove the { and } characters
String[] numberSets = numberSetsWithoutBraces.split(",");
List<Double> averages = new ArrayList<Double>();
for (String numberSet : numberSets) {
List<Integer> numbers = getNumbersInNumberSet(numberSet);
double average = getAverage(numbers);
averages.add(average);
}
double averageOfAverages = getAverageOfDoubles(averages);
List<String> numberSetsMeetingCriteria = new ArrayList<String>();
for (int i = 0; i < averages.size(); i++) {
double average = averages.get(i);
if (average >= averageOfAverages) {
numberSetsMeetingCriteria.add(numberSets);
}
}
String result = "";
result += "{";
for (String numberSetMeetingCriteria : numberSetsMeetingCriteria) {
result += "{";
result += numberSetMeetingCriteria;
result += "}";
result += ",";
}
// Remove final comma
if (result.charAt(result.length() 1) == ',') {
result = result.substring(0, result.length() 2);
}
result += "}";
System.out.println(result);
}
private static double getAverage(List<Integer> numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
int length = numbers.size();
return total / (double) length;
}
private static double getAverageOfDoubles(List<Double> doubles) {
double total = 0.0;
for (double doubleNumber : doubles) {
total += doubleNumber;
}
int size = doubles.size();
return total / (double) size;
}
private static List<Integer> getNumbersInNumberSet(String numberSet) {
String[] numberStrings = numberSet.split(",");
List<Integer> result = new ArrayList<Integer>();
for (String numberString : numberStrings) {
result.add(Integer.parseInt(numberString));
}
return result;
}
}
problem:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method add(String) in the type List<String> is not applicable for the arguments (String[])
part of code having a problem with:
if (average >= averageOfAverages) {
numberSetsMeetingCriteria.add(numberSets);
}
Any help would be very appreciated, I am stuck and can't figure out why it won't work.

Re: Need help with an error
 04172012, 04:41 AM #3Member
 Join Date
 Apr 2012
 Posts
 5
 Rep Power
 0
Re: Need help with an error
so what do you recommend i do?

Re: Need help with an error
 04172012, 04:47 AM #5Member
 Join Date
 Apr 2012
 Posts
 5
 Rep Power
 0

Re: Need help with an error
Please clarify your question as much as possible. The only specific problem you've told us is about your error. You haven't yet mention what you're trying to do with the erroneous line of code, what made you write it, where specifically you're stuck now, or even what the program is supposed to do. So until you do that, this is about the best any of us can do.
Last edited by Fubarable; 04172012 at 05:01 AM.
 04172012, 05:05 AM #7Member
 Join Date
 Apr 2012
 Posts
 5
 Rep Power
 0
Re: Need help with an error
Write a Java program to read a nonempty collection of number sets. Each number set is nonempty and only contains intefers and is written in {,} notation. The collection is alos written in {,} notation. The program must write out the collection of those number sets whose (integer) average is less then the (intefer) average of all of the (integer) averages of the number sets in the collection. The outputs must also be {,} notation. There should be NO prompts and No legends.

Re: Need help with an error
OK, and now understanding your error that you initially posted and why it is not valid, can you tell us where are you currently stuck? Please post a few sentences describing your problems, your questions, and misunderstandings. Please post your latest code, comment it a bit if possible, and also use [code] [/code] tags so it is readable.
 04172012, 05:15 AM #9Member
 Join Date
 Apr 2012
 Posts
 5
 Rep Power
 0
Re: Need help with an error
I literally just have no idea what to do, i searched for an answer and
Java Code:import java.util.ArrayList; import java.util.List; public class FiltAvg { public static void main(String[] args) { String numberSetsString = args[0]; // we assume there is always a number set passed in String numberSetsWithoutBraces = numberSetsString.substring(1, numberSetsString.length() 1); // remove the { and } characters String[] numberSets = numberSetsWithoutBraces.split(","); List<Double> averages = new ArrayList<Double>(); for (String numberSet : numberSets) { List<Integer> numbers = getNumbersInNumberSet(numberSet); double average = getAverage(numbers); averages.add(average); } double averageOfAverages = getAverageOfDoubles(averages); List<String> numberSetsMeetingCriteria = new ArrayList<String>(); for (int i = 0; i < averages.size(); i++) { double average = averages.get(i); if (average >= averageOfAverages) { numberSetsMeetingCriteria.add(numberSets); } } String result = ""; result += "{"; for (String numberSetMeetingCriteria : numberSetsMeetingCriteria) { result += "{"; result += numberSetMeetingCriteria; result += "}"; result += ","; } // Remove final comma if (result.charAt(result.length() 1) == ',') { result = result.substring(0, result.length() 2); } result += "}"; System.out.println(result); } private static double getAverage(List<Integer> numbers) { int total = 0; for (int number : numbers) { total += number; } int length = numbers.size(); return total / (double) length; } private static double getAverageOfDoubles(List<Double> doubles) { double total = 0.0; for (double doubleNumber : doubles) { total += doubleNumber; } int size = doubles.size(); return total / (double) size; } private static List<Integer> getNumbersInNumberSet(String numberSet) { String[] numberStrings = numberSet.split(","); List<Integer> result = new ArrayList<Integer>(); for (String numberString : numberStrings) { result.add(Integer.parseInt(numberString)); } return result; } }

Re: Need help with an error
Don't search for an answer, and don't borrow an answer  you gain little by doing this, and what you really need is the understanding to be able to write it yourself. We also can't really help you much when you post borrowed code, since you will have little understanding of the code to begin with.
my teacher skipped out on this entirely, ...
...and i have no idea what to do. i tried the above code and got the error i asked about. my question is basically, where would i start/what should i do?
First you must understand the problem as that's 8090% of solving the problem. Please read my link below on So, You Need to Write a Program but Don't Know How to Start.
Next you need to understand Java. If you don't understand something from class, then it's your responsibility to learn it on your own, and the Java tutorials are a good place to start: The Java Tutorials: The Really Big Index. And if any of the tutorials confuse you, then please come in here and ask.
Please come on back when you can ask a more specific question and when you have your own code to post.
Best of luck!
Similar Threads

java out of memory errorheap space error
By elsanthosh in forum NetBeansReplies: 4Last Post: 06152010, 09:31 AM 
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03102010, 02:53 PM 
Thread: Error 500Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12052009, 08:28 AM 
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10262009, 05:38 PM 
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01232009, 09:53 AM
Bookmarks