readLine problem

Hi guys,

I have a slight problem here. My code is simple and is as follows:

import java.io.*;

public class Average {

public static void main(String[] args) throws IOException {

double numbers = 0.0;

System.out.println("Please enter 5 numbers");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

for (int i=0; i<5; i++) {

numbers += Double.parseDouble(br.readLine());

}

System.out.println("The average of your numbers is " + numbers/5);

}

}

My problem is that readLine keeps remembering the very first double read. For example, if I input, 1,2,3,4,5, I get an average of 1.0

This is obviously wrong, but it is because the first time I enter 1, the program remembers that and always goes 1+1+1+1+1. Which is why I get and average of 1.0

Strange thing is that if I put System.out.println("") after the line in the for loop, it works.

Any ideas?

Thanks

[992 byte] By [jahanuma] at [2007-11-27 9:57:29]
# 1
One thread is enough!Answered here: http://forum.java.sun.com/thread.jspa?threadID=5192648
prometheuzza at 2007-7-13 0:27:46 > top of Java-index,Java Essentials,New To Java...
# 2

I have tried your code on my console but it is not like what ever you are saying

IT gives Numberformat exception as I predicted first

The method readLine takes the whole like as one input point.

as , was there in 1,2,3,..... sequence so it is obviously a numberformatexception

the second thing is that u cannot use read () method as it returns only one character

I suggest u to please read the basics of Java

Have a nice time

Bye

There is no strange thing in java . It is natural and clear OOP language

PRASHANTH

PRSHANTHa at 2007-7-13 0:27:46 > top of Java-index,Java Essentials,New To Java...
# 3
Where am I using the read() method? and I get no exception. I do not input data as 1,2,3,4,5. I enter them one at a time. I really don't understand what you are saying.
jedi786a at 2007-7-13 0:27:46 > top of Java-index,Java Essentials,New To Java...
# 4
I also get no errors at all and the output is correct 1 2 3 4 5 gives me an output of 3. Works fine.
pberardi1a at 2007-7-13 0:27:46 > top of Java-index,Java Essentials,New To Java...