Code doesn't work inside "for" statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Coreyja
    New Member
    • Nov 2008
    • 4

    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.

    Code:
    Scanner reader = new Scanner(new FileReader("test.txt"));
            for (i=0;i > 10;i++){
                read = reader.nextDouble();
                System.out.println(read);
            }
    Here is the text file that it is reading from.

    test.txt
    Code:
    12.345
    13.345
    14.345
    15.345
    16.345
    17.345
    18.345
    19.345
    20.345
    21.345
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Code:
    for (i=0;i > 10;i++){ 
       read = reader.nextDouble(); 
       System.out.println(read); 
    }
    Look at your code again; a for loop works like this:

    Code:
    for (<initialize>; <condition>; <increment>)
       <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,

    Jos

    Comment

    • Coreyja
      New Member
      • Nov 2008
      • 4

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

      Comment

      Working...