Averaging method errors

Averaging from ArrayList

Page 1 of 1

6 Replies - 976 Views - Last Post: 09 September 2009 - 08:11 PM Rate Topic: -----

#1 Overachiever   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 36
  • Joined: 09-September 09

Averaging method errors

Posted 09 September 2009 - 04:51 PM

This is my first post asking for help, so please, if there's something I should do differently, let me know.

I am getting 7 compiler errors with the following method:

  private String getNumbersAverage(ArrayList list) {
    double numbersAverage = 0;
    for (i=0, i < (list.size()), i++) {
      numbersAverage += (list.get(i));
    }
    numbersAverage = (numbersAverage / (list.size()));
    String s = Double.toString(numbersAverage);
    return s;
  }


The errors are after each of the following:

i < (list.size()) not a statement
i++ ';' expected
i++) ';' expected
(list.size()) ')' expected

numbersAverage = (numbersAverage / (list.size()));
<indentifier> expected (right after the first numbersAverage)

return ';' expected

Also, right before return, it has the error "illegal start of type"

Could anyone please explain to me why I am getting all of these errors for this simple method? I'm sure there's something that my newbish mind is missing. :)

If you need all the code to find the reason, please tell me.

Is This A Good Question/Topic? 0
  • +

Replies To: Averaging method errors

#2 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Averaging method errors

Posted 09 September 2009 - 05:06 PM

well, you need to declare i and int
for (i=0, i < (list.size()), i++) { int i = 0 that should take care of that. And btw, you can use just i<list.size() dont need to put it in parenthese.

Change the return type to a double, and just return numbersAverage?
Dont see why is has to be a string, and if it does will this work yes?
String s = String.valueOf(numbersAverage);


Hope this helps!
Was This Post Helpful? 1
  • +
  • -

#3 Overachiever   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 36
  • Joined: 09-September 09

Re: Averaging method errors

Posted 09 September 2009 - 05:54 PM

Ok, so I changed it to this:

  private String getNumbersAverage(ArrayList list) {
    double numbersAverage = 0;
    for (int i=0, i < list.size(), i++) {
      numbersAverage += (list.get(i));
    }
    numbersAverage = (numbersAverage / (list.size()));
    String s = String.valueOf(numbersAverage);
    return s;
  }


...that helped a bit, but I'm still getting weird errors.

i < list.size() ';' expected ...right after i and after size(
i < list.size() > expected ...right after size
i < list.size() illegal start of expression ...right after size()
i < list.size(), ')' expected ...right after the comma
i++ illegal start of expression ...right after the second +

Also, I put the return type as a String because I'm using it to display the average in a JTextField.

This post has been edited by Overachiever: 09 September 2009 - 05:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Averaging method errors

Posted 09 September 2009 - 05:59 PM

And in the for statement, it is ";" not ","

And assuming your ArrayList contains double, you will have to cast them

   	for (int i = 0; i < list.size(); i++) {	  
			numbersAverage += (Double) list.get(i);	
		}	
 

Was This Post Helpful? 1
  • +
  • -

#6 Overachiever   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 36
  • Joined: 09-September 09

Re: Averaging method errors

Posted 09 September 2009 - 06:05 PM

It's working now. Thanks for helping out.
Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Averaging method errors

Posted 09 September 2009 - 06:09 PM

gah, held shift key when I hit the semi colon my bad. See! Learn lesson not to copy and paste! :D
yeah, I meant to do that ;)

Glad its working now
Was This Post Helpful? 1
  • +
  • -

#8 LynnL   User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 109
  • Joined: 13-April 09

Re: Averaging method errors

Posted 09 September 2009 - 08:11 PM

View Postpbl, on 9 Sep, 2009 - 04:59 PM, said:

And in the for statement, it is ";" not ","

And assuming your ArrayList contains double, you will have to cast them

   	for (int i = 0; i < list.size(); i++) {	  
			numbersAverage += (Double) list.get(i);	
		}	
 

You always have a final touch up
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1