Results 1 to 4 of 4
  1. #1
    lenny0 is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Counting number of entries in a file

    Hi all,

    I am having problems with counting the number of entries (doubles) in a file as specified by user input. The file opens fine and begins to count, however the accumulator never progresses past 1. also when counting a file with no entries, it never returns as zero, but always 1.

    Below is the code of the offending method.

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Calculate
    {
      static String fName; 
      static int count;
      static double num[];
      static double m;
      static double var;
    
       /**the countData method returns the number of entries of type double in file.
          @return The value in the entries filed.
       */
    
      public static int countData() throws IOException
      { 
        String fileName = "";      //file name as input by user
        fName = fileName;        //passes fileName reference to class variable fname
        int entries = 0;	       //accumulator for number of file entries
        count = entries;            //passes entries reference to class variable count
    
        //create instance of scanner class to capture user input
        Scanner keyboard = new Scanner(System.in);
        //prompt user for input
        System.out.print("Enter file name: ");
        //assign reference to local variable
        fileName = keyboard.nextLine();
        //create an instance of file class to reference fileName
        File file = new File (fileName);
        //create instance of scanner class to read from file
        Scanner inputFile = new Scanner(file);
        //increment the accumulator while more entries exist in the file
        while (inputFile.hasNextDouble())
    	  inputFile.nextDouble();
              entries++;
        //close the file
        inputFile.close();
        //if less than 2 entries are in file, prompt user that more are required
        if (entries <2)
           System.out.println("The file " +fileName +" needs to contain more than 1 entry");
        //verifying accumulator is working, which it is not!
        System.out.println("number of entries is: " +entries);
        return entries;
      }//end of countData method
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Java Code:
    while (inputFile.hasNextDouble())
    	  inputFile.nextDouble();
    That is your loop. Indenting the next line does not make it automagically inside the loop.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    edit: Junky is right. Wrap your loop in curly braces if you have more than 1 statement(or always do it so you don't run into errors like this)
    Last edited by sunde887; 05-10-2011 at 03:39 AM.

  4. #4
    lenny0 is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks Juky. I have been staring at this for so long that I was blind to this. Below is the fix.

    while (inputFile.hasNextDouble())
    {
    inputFile.nextDouble();
    entries++;
    }

Similar Threads

  1. Counting total number of images in a website
    By sharifiit in forum Networking
    Replies: 0
    Last Post: 04-30-2011, 08:20 AM
  2. Replies: 9
    Last Post: 03-06-2011, 03:00 AM
  3. Replies: 1
    Last Post: 10-24-2009, 02:18 AM
  4. counting number of lines of system.out
    By IYIaster in forum New To Java
    Replies: 1
    Last Post: 07-21-2009, 12:37 AM
  5. Counting the number of columns in a 2D array,
    By KalEl in forum New To Java
    Replies: 9
    Last Post: 10-21-2008, 05:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •