This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to determine the length of file contents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to determine the length of file contents" Watch "How to determine the length of file contents" New topic
Author

How to determine the length of file contents

Mohit Hyd Kumar
Greenhorn

Joined: Jul 31, 2011
Posts: 1
Hi all,
I am writing a program for binary search on contents of a file.

The steps involved in it are:

1. Have to read a file[Inputing the filename from the user] - using bufferedReader.readLine() method[bufferedReader = new BufferedReader(new InputStreamReader(new FileInputSteam(filename)))]
2. Have to search the number[entered by user] in the contents of file [using Binary Search]
3. Have read from the file and input the contents in a int array.

The problem arises here:
The array i formed and then sorted[2 different methods] have some zero values[as I am not able to determine actually how many numbers are there in the file, so took an arbitrary int array of size 20, but the file contains only 6 numbers, hence there are 14 "0" values].
However besides the values, the method works flawlessly

Any solutions how to determine how many numbers are present in the file[cannot use fileInputStream.read() == -1 as stated above].
Thanks in advance
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
maybe you should use ArrayList ?
James E Baker
Greenhorn

Joined: Jul 28, 2011
Posts: 23
Mohit Hyd Kumar wrote:
Any solutions how to determine how many numbers are present in the file[cannot use fileInputStream.read() == -1 as stated above].
Thanks in advance


So, the user will never enter '0' as a valid value?

If you want to count how many values in an array are NOT zero, then you could do


Is that what you're looking to do?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to determine the length of file contents
 
Similar Threads
totally screwed newbie, need assignment help
sorting
count occurrence of number in array
2d arrays
Problems reading from a .txt file