470,612 Members | 2,270 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,612 developers. It's quick & easy.

Code doesn't work inside "for" statement

I am new to Java and would greatly appreciate any help. Here is some code I am working on as basically a proof of concept. I am using a "throws FileNot FoundException". The problem is that when i run the program nothing happens. There are no errors but there is also no output. This same code worked without the "for" statement for just the first number.

Expand|Select|Wrap|Line Numbers
  1. Scanner reader = new Scanner(new FileReader("test.txt"));
  2.         for (i=0;i > 10;i++){
  3.             read = reader.nextDouble();
  4.             System.out.println(read);
  5.         }
Here is the text file that it is reading from.

Expand|Select|Wrap|Line Numbers
  1. 12.345
  2. 13.345
  3. 14.345
  4. 15.345
  5. 16.345
  6. 17.345
  7. 18.345
  8. 19.345
  9. 20.345
  10. 21.345
Mar 22 '09 #1
2 1534
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. for (i=0;i > 10;i++){ 
  2.    read = reader.nextDouble(); 
  3.    System.out.println(read); 
Look at your code again; a for loop works like this:

Expand|Select|Wrap|Line Numbers
  1. for (<initialize>; <condition>; <increment>)
  2.    <statements>;
First the <initialize> section is executed once; then the <condition> is tested; if it has a true value then <statements> are executed and finally the <increment> part is executed. Then the <condition> is tested again and the cycle repeats as long as <condition> is true; otherwise the loop finishes.

Look at your <condition>: it isn't true the first time it is tested (0 < 10) so your loop terminated immediately. You probably wanted to write i < 10.

kind regards,

Mar 22 '09 #2
Oh wow! Even I should have been able to catch that one! Thanks alot for the help tho!
Mar 22 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by New Guy | last post: by
1 post views Thread by Doug Telford | last post: by
28 posts views Thread by Andre | last post: by
2 posts views Thread by Deniz Dogan | last post: by
5 posts views Thread by Dave Navarro | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.