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?

[978 byte] By [jahanuma] at [2007-11-27 9:57:28]
# 1
> > Any ideas?Your code works for me! Are you sure you compile each time you make a change? java AveragePlease enter 5 numbers12345The average of your numbers is 3.0Message was edited by: sabre150
sabre150a at 2007-7-13 0:27:45 > top of Java-index,Core,Core APIs...
# 2
That is so weird. I even deleted my .class file and it still gives me the wrong average. I am so annoyed :@
jahanuma at 2007-7-13 0:27:45 > top of Java-index,Core,Core APIs...
# 3

Hey ,

Please avoid to use any proprietary editor if u are using for coding java which facilate coding and execution enviornment also (e.g. JCreator )

Use simple notepad and run it on cosole.

I have tried it 2 ways

with editor it gives same problem as u stated.

and

when used with normal nitepad and console it working okey

PRSHANTHa at 2007-7-13 0:27:45 > top of Java-index,Core,Core APIs...