how do i count the number of items in the text file in java

Hi all
I dont know how close i am i think i am close but could be way off, what i have is a text file and i need to count the number of items in the text file and return the value this is my code so far, the 1 is there as a place holder so far and these are the errors i am getting

.\Calculate.java:18: incompatible types
found   : int
required: java.lang.Double
     Double Count = 0;
                    ^
.\Calculate.java:20: cannot find symbol
symbol  : method hasNext()
location: class java.io.FileReader
     while (file.hasNext())
                ^
.\Calculate.java:22: cannot find symbol
symbol  : method nextLine()
location: class java.io.FileReader
             file.nextLine();
                 ^
.\Calculate.java:23: cannot find symbol
symbol  : variable count
location: class Calculate
             count ++;
import java.io.*;
import java.util.Scanner;

public class Calculate
{
    public static int countData(String fileName)
 {

     FileReader file = new FileReader(fileName); //This will allower the filerReader to know the name of the file been passed from the Calcularetest File 
     BufferedReader input = new BufferedReader(file); //This will Buffer the infomation in the file 

     Double Count = 0;

     while (file.hasNext())
	 {
	     file.nextLine();
	     count ++;
	 }


    
     return 1;
  }

Open in new window

awolarczukAsked:
Who is Participating?
 
CEHJCommented:
You can't. It will only count linefeeds. That's how 'lines' are defined, not by what characters appear in the file.

If your objective is to do this to fill an array, then this isn't necessary anyway. Just use a List and turn it into an array.

In either case, linefeeds are read unless you use fixed record lengths
0
 
CEHJCommented:
Not far off. You can simplify somewhat:
public static int countData(String fileName) throws IOException {
        Scanner input = new Scanner(new File(fileName));

        int count = 0;

        while (input.hasNextLine()) {
            count++;
	    input.nextLine();
        }

	input.close();
        return count;
    }

Open in new window

0
 
awolarczukAuthor Commented:
cool mate i got most of it working i am jusr getting one error now




CalculateTest.java:35: unreported exception java.io.IOException; must be caught or declared to be thrown
       Calculate.countData(filename); //passing the file name that has been given bu the user to the Calulate file
                          ^
1 error

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJCommented:
You need to catch the exception
try {
    Calculate.countData(filename);
}
catch(IOException e) {
    e.printStackTrace();
}

Open in new window

0
 
CEHJCommented:
(And you'll want to assign the result of countData to a variable)
0
 
awolarczukAuthor Commented:
mate i have done all the things you just said

now getting this



CalculateTest.java:39: cannot find symbol
symbol  : class IOException
location: class CalculateTest
       catch (IOException e)
              ^
1 error


try
	   {
       Calculate.countData(filename); //passing the file name that has been given bu the user to the Calulate file 
       }
       catch (IOException e)
	   {
	       e.printStackTrace();
	   }

Open in new window

0
 
CEHJCommented:
import java.io.IOException;
0
 
awolarczukAuthor Commented:
bugger i am a loser something so simple sorry mate
0
 
CEHJCommented:
No problem. Working?
0
 
awolarczukAuthor Commented:
mate when i run a file in that has no data in it for some reason it is counting to three where have i gone wrong here
Scanner input = new Scanner (new File(fileName));

     int Count = 0;

     while (input.hasNextLine())
	 {
	     Count++;
	     input.nextLine();
	     System.out.println (Count);
	     
	 }
    
     input.close();
     if (Count < 1)
	 System.out.println ("The File " + fileName + " " + "needs to contain more then 1 number");
      else     
	  System.out.println (Count);
     return Count;
  }

Open in new window

0
 
CEHJCommented:
It's counting line feed characters, not data
0
 
awolarczukAuthor Commented:
how i can make it count the lines of data in the file
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.