[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

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

0
awolarczuk
Asked:
awolarczuk
  • 7
  • 5
1 Solution
 
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
 
CEHJCommented:
You need to catch the exception
try {
    Calculate.countData(filename);
}
catch(IOException e) {
    e.printStackTrace();
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now