Results 1 to 10 of 10
  1. #1
    Java-asker is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with an error

    Quote Originally Posted by Java-asker View Post
    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.
    The error is telling you exactly what's wrong. You're trying to call a method that expects a String parameter by passing in an array of String, and that will never work.

  3. #3
    Java-asker is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help with an error

    so what do you recommend i do?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with an error

    Quote Originally Posted by Java-asker View Post
    so what do you recommend i do?
    For starters, only call methods as they are intended to be called. If you need to add a single String to a List via the add method, pass in only one String, not an array of String..

  5. #5
    Java-asker is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help with an error

    Quote Originally Posted by Fubarable View Post
    For starters, only call methods as they are intended to be called. If you need to add a single String to a List via the add method, pass in only one String, not an array of String..
    I was asking specifically about the program so it will work. I am really trying to get this thing finished for my class

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with an error

    Quote Originally Posted by Java-asker View Post
    I was asking specifically about the program so it will work. I am really trying to get this thing finished for my class
    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; 04-17-2012 at 05:01 AM.

  7. #7
    Java-asker is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default 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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with an error

    Quote Originally Posted by Java-asker View Post
    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.
    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.

  9. #9
    Java-asker is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default 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;
    }
    }
    that was the best answer i could find. 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?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with an error

    Quote Originally Posted by Java-asker View Post
    I literally just have no idea what to do, i searched for an answer and ...
    ...
    ... that was the best answer i could find.
    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, ...
    For this you should contact your teacher or your school to find out what you're missing.


    ...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?
    We don't do very well with these types of questions, and we're much better at answering specific well designed questions.

    First you must understand the problem as that's 80-90% 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

  1. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 09:31 AM
  2. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  5. Replies: 3
    Last Post: 01-23-2009, 08:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •