Results 1 to 4 of 4
- 05-10-2011, 03:22 AM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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 }
- 05-10-2011, 03:27 AM #2
That is your loop. Indenting the next line does not make it automagically inside the loop.Java Code:while (inputFile.hasNextDouble()) inputFile.nextDouble();
- 05-10-2011, 03:34 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 05-10-2011, 03:40 AM #4
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Counting total number of images in a website
By sharifiit in forum NetworkingReplies: 0Last Post: 04-30-2011, 08:20 AM -
Help w/ java programming assignment counting number of spaces
By clemsontigers in forum New To JavaReplies: 9Last Post: 03-06-2011, 03:00 AM -
log4j: Multiple entries on Tomcat stdout file using log.error
By pbs in forum Web FrameworksReplies: 1Last Post: 10-24-2009, 02:18 AM -
counting number of lines of system.out
By IYIaster in forum New To JavaReplies: 1Last Post: 07-21-2009, 12:37 AM -
Counting the number of columns in a 2D array,
By KalEl in forum New To JavaReplies: 9Last Post: 10-21-2008, 05:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks